接口定义增强

JDK 1.8开始允许在接口中通过defaultstatic定义普通方法。
这样做的意义在于,若接口中新增了一个方法,并且所有子类的实现方式相同,可以避免大量的重复代码。

class IMessage {
    public void print();
    default void commonMethod() {
        System.out.println("通过default在接口中定义普通方法");
    }
    static void commonMethod2() {
        System.out.println("通过static在接口中定义普通方法");
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/liyue-sqsf/p/8921405.html
今日推荐