JDK8新增接口的默认方法与静态方法

时间:2023-03-09 19:46:20
JDK8新增接口的默认方法与静态方法

JDK8之前,interface中可以定义常量和抽象方法,访问修饰符是public。

public interface A {
/** a1和a2写法是等价的 */
public static final int a1 = 0; int a2 = 0; /** methodA1和methodA2写法是等价的 */
public abstract void methodA1(); void methodA2();
}

JDK8起,允许我们在interface中使用static和default修饰方法(使用这两种修饰符中其一就不能使用abstract修饰符),从而方法具有方法体。

public interface A {
/** default访问修饰符修饰的方法 */
default void methodB1() {
System.out.println("this is default method");
} /** static修饰符修饰的方法 */
public static void methodB2() {
System.out.println("this is static method");
}
}

default修饰的方法,通过接口的实现类的对象调用;static修饰的方法,直接通过接口名调用。

public class Test implements A{
public static void main(String[] args) { /** 接口的default方法,通过接口的实现类的对象调用 */
Test test = new Test();
test.methodB1(); /** 接口的静态方法,通过接口名调用 */
A.methodB2();
}
}

由于java支持一个实现类可以实现多个接口,如果多个接口中存在同样的static和default方法会怎么样呢?

  • 如果有两个接口中的static方法一模一样,并且实现类同时实现了这两个接口,此时并不会产生错误,因为jdk8只能通过接口类调用接口中的静态方法,所以对编译器来说是可以区分的。
  • 如果两个接口中定义了一模一样的default方法,并且一个实现类同时实现了这两个接口,那么必须在实现类中重写默认方法,否则编译失败。
public interface A {
/** default访问修饰符修饰的方法 */
default void methodB1() {
System.out.println("this is default method -- InterfaceA");
} /** static修饰符修饰的方法 */
public static void methodB2() {
System.out.println("this is static method -- InterfaceA");
}
} public interface B{
/** default访问修饰符修饰的方法 */
default void methodB1() {
System.out.println("this is default method -- InterfaceB");
} /** static修饰符修饰的方法 */
public static void methodB2() {
System.out.println("this is static method -- InterfaceB");
}
} public class Test implements A,B{
/** 由于A和B中default方法一样,所以这里必须覆盖 */
@Override
public void methodB1() {
System.out.println("this is Overriding methods");
} public static void main(String[] args) { /** 接口的default方法,通过接口的实现类的对象调用 */
Test test = new Test();
test.methodB1(); /** A接口的静态方法,通过接口名调用 */
A.methodB2();
/** B接口的静态方法,通过接口名调用 */
B.methodB2();
}
}

运行结果:

  JDK8新增接口的默认方法与静态方法

参考:https://blog.****.net/aitangyong/article/details/54134385