
之前Java接口中的方法默认都是public abstract,成员变量默认都是public static final,偶然发现接口中可以有default类型的方法,才知道java8中接口可以有自己的实现了。那么jdk1.8究竟对接口做了哪些修改呢? (1) 增加default方法。default方法作用范围也是public,只是有了具体实现的方法体。对已有的接口,如果想对接口增加一个新方法,那么需要对所有实现该接口的类进行修改。而有了default方法,可以解决该问题。 (2) 新增static方法。static修饰的方法也是非抽象方法,使用同类的静态方法一样,给方法的调用带来了方便。程序入口main方法也是static,现在接口也可以运行了。 例如下面在InterfaceA中定义了一个default方法,一个static方法:
public interface InterfaceA {
default String f(){
return"this is InterfaceA";
}
static String getName(){
return "InterfaceA";
}
}那么在实现类中可以直接调用接口的default方法,通过接口名.方法名调用static方法
public class ImplClass implements InterfaceA {
public static void main(String[] args) {
System.out.println(new ImplClass().f());
System.out.println(InterfaceA.getName());
}
}结果:
但是注意,如果一个类同时实现了两个拥有相同方法签名(相同的方法名、参数)、返回类型的default方法后,需要在类中重写default方法,否则编译器会因为不知道应该调用哪一个接口中的default方法而报错 再定义一个InterfaceB接口,和InterfaceA接口拥有相同方法签名、返回类型的f()
public interface InterfaceB {
default String f(){
return "this is InterfaceB";
}
}ImplClass同时实现InterfaceA和InterfaceB,这时编译器会报错:
重写接口中default方法后,编译器会执行重写后的方法:
public class ImplClass implements InterfaceA,InterfaceB {
@Override
public String f(){
return "this is ImplClass";
}
public static void main(String[] args) {
System.out.println(new ImplClass().f());
}
}结果:
如果一个类同时继承的父类和实现的接口拥有相同签名、返回类型时,当该类未重写方法,直接调用时,将会调用父类中的方法: 再定义一个父类FatherC,拥有和InterfaceA同签名、返回类型的方法f()
public class FatherC {
public String f(){
return "this is FatherC";
}
}ImplClass实现InterfaceA的同时继承FatherC,未重写方法f(),直接调用时,将会调用父类中的方法:
public class ImplClass extends FatherC implements InterfaceA {
public static void main(String[] args) {
System.out.println(new ImplClass().f());
}
}结果:
接口已经越来越向类靠近了,所以现在接口和抽象类的区别有:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168701.html原文链接:https://javaforall.cn