44.接口组成更新

1.概述

 2.接口中的默认方法

默认方法的出现解决了java在一些老接口更新时,之前的实现类不必一定都要追加重写新增的方法,例如JKD11.0版本里面List接口新增了很多方法,但是,之前的ArrayList等子类就不必跟着强制更新重写List接口的新方法,在需要使用时可以手动追加重写;这在我们今后开发中也很实用;可以用于现有接口的升级而不破坏原来的代码

public interface FatherInterface {
    void show1();
    void show2();
    default void show3(){
        
    };
}
public class Son implements FatherInterface {
    @Override
    public void show1() {
        System.out.println("非default方法必须重写");
    }

    @Override
    public void show2() {
        System.out.println("非default方法必须重写");
    }

//    @Override
//    public void show3() {
//        System.out.println("default方法可以不用重写");
//    }
}

3.接口中的静态方法

public interface FatherInterface {
    void show1();
    void show2();
    default void show3(){
        System.out.println("我是接口里面的default方法");
    };
     static void show4(){
        System.out.println("接口中的静态的方法执行了");
    }
}
   public static void main(String[] args) {
//        只能通过接口名调用
        FatherInterface.show4();
    }

4.接口中的私有方法

猜你喜欢

转载自www.cnblogs.com/luzhanshi/p/13199413.html