JAVA8 接口的增强

323 阅读1分钟

JAVA8 接口的增强: 在Java8之前的接口只能定义抽象方法,即不能有具体的实现,但是在Java8之后,接口中可以定义具体的方法实现,但是只能是default或者static类型的方法。

这样实现的好处:可以对于解决某些情况下,接口中的某些方法在子类之中的实现是一样的,这样就能减少重复代码,完成代码的复用;

public class JAVA_8_Interface {
    public static void main(String[] args){
    JavaInterface javaInterface = new InterfaceClass();
    javaInterface.show();
    JavaInterface.display();
    }
}

class InterfaceClass implements  JavaInterface{
}

interface JavaInterface{
    default void show(){
        System.out.println("this is show method");
    }
    static void display(){
        System.out.println("this is display method");
    }
}