美文网首页
Java8的接口增强

Java8的接口增强

作者: 胖瘦馒头 | 来源:发表于2018-07-16 21:21 被阅读0次

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");
    }
}

相关文章

网友评论

      本文标题:Java8的接口增强

      本文链接:https://www.haomeiwen.com/subject/srlipftx.html