Abstract&Interface
-
不能new这个抽象类,只能靠继承他的子类去实现它 约束
-
抽象类中可以写普通的方法
-
抽象方法必须在抽象类中
-
抽象方法:只有方法名,没有方法的实现
接口
-
interface 定义关键字
-
接口中的所有定义都是抽象的
-
JDK8版本开始,允许在接口体中定义static方法,允许使用default方法,但不可以定义default的static方法
-
JDK9版本开始,允许在接口体中定义private方法(通常搭配default方法使用)
interface Com{
public final int MAX = 100;
public abstract void add();
public abstract float sum(float x,float y);
public default void max(int a,int b){
System.out.println(Max(a,b));
}//default方法,可以省略public
private int Max(int a,int b){
return a>b?a:b;
}
}
-
-
-
利用接口实现多继承
-
例
public interface UserService{
void add();
}
public interface TimeService {
void timer();
}
public class UserServiceImpl implements UserService,TimeService{
void add(){}
void timer(){}
}
-

浙公网安备 33010602011771号