学习笔记之接口组成与更新

接口组成更新

接口组成更新概述

接口组成

  • 常量
    • public static final
  • 抽象方法
    • public abstract
  • 默认方法(JDK8)
  • 静态方法(JDK8)
  • 私有方法(JDK9)

接口中默认方法

在接口需要更新添加新的抽象方法时,每一个实现类都需要去重写这一个抽象方法,虽然可以定义新的接口去继承需要更新的接口,但是随着更新,代码体系会变得非常庞大,不利于维护。
格式:

  • public default返回值类型方法名(参数列表){}
  • 例:public default void show(){}

默认方法不是抽象方法,所以不强制重写,但是可以被重写,重写时去掉default关键字
public可以省略,default不能省略

例如:

public interface MyInterface {
    void show1();
    void show2();
    default void show3(){
        System.out.println("默认方法");
    }
}
public class MyInterfaceImplOne implements MyInterface {
    @Override
    public void show1() {
        System.out.println("show1");
    }

    @Override
    public void show2() {
        System.out.println("show2");
    }
}

重写show3方法:

public class MyInterfaceImplTwo implements MyInterface {
    @Override
    public void show1() {
        System.out.println("Oneshow");
    }

    @Override
    public void show2() {
        System.out.println("Twoshow");
    }

    //重写时省略default关键字
    @Override
    public void show3() {
        System.out.println("Threeshhow");
    }
}

Demo:

public class Demo {
    public static void main(String[] args) {
        MyInterface mi = new MyInterfaceImplOne();
        mi.show1();
        mi.show2();
        mi.show3();
    }
}

接口中的静态方法

格式:

  • public static 返回值类型方法名(参数列表){}
  • 例:public static void show(){}

静态方法只能通过接口名调用,不能通过实现类名或者对象名调用

public可以省略,static不能省略

public interface MyInter {
    void show();
    default void method(){
        System.out.println("MyInter 中的默认方法");
    }
    static void test(){
        System.out.println("MyInter 中的静态方法");
    }
}
public interface Flyable {
    static void test(){
        System.out.println("Flyable 中的静态方法");
    }
}
public class InterImpl implements MyInter {
    @Override
    public void show() {
        System.out.println("show");
    }

}
public class Demo {
    public static void main(String[] args) {
        MyInter mi = new InterImpl();
        mi.show();
        mi.method();

        MyInter.test();
        Flyable.test();
    }
}

接口中私有方法

JDK 9 中新增了带有方法体的私有方法,这其实再 JDK 8 中就埋下了伏笔:JDK 8允许在接口中定义带方法体的默认方法和静态方法,这样可能就会引发一个问题:当两个默认方法或者静态方法中包含一段相同的代码实现时,程序必然考虑将这段实现代码抽取成一个共性方法,儿这个共性方法时不需要让别人使用的,因此私有给隐藏起来,这就是JDK 9增加私有方法的必然性。

格式 1:

  • private返回值类型方法名(参数列表){}
  • 例:private void method(){}

格式 2:

  • private static 返回值类型方法名(参数列表){}
  • 例:private static method(){}

默认方法可以调用私有的静态方法和非静态方法

静态方法只能调用私有的静态方法

package 接口中的私有方法;

public interface Inter {
    default void method1(){
        System.out.println("默认方法一开始执行");
//        System.out.println("1");
//        System.out.println("2");
        method();
        show();
        System.out.println("默认方法一执行结束");
    }

    default void method2(){
        System.out.println("默认方法二开始执行");
//        System.out.println("1");
//        System.out.println("2");
        method();
        show();
        System.out.println("默认方法二执行结束");
    }
    //私有
    private void method(){
        System.out.println("1");
        System.out.println("2");
    }

    static void show1(){
        System.out.println("静态方法一开始执行");
//        System.out.println("1");
//        System.out.println("2");
        show();
        System.out.println("静态方法一执行结束");
    }

    static void show2(){
        System.out.println("静态方法二开始执行");
//        System.out.println("1");
//        System.out.println("2");
        show();
        System.out.println("静态方法二执行结束");
    }
    //私有静态
    private static void show(){
        System.out.println("1");
        System.out.println("2");
    }
}

public class Intermpl implements Inter {
}
public class Demo {
    public static void main(String[] args) {
        Inter i = new Intermpl();
        i.method1();
        System.out.println("--------------");
        i.method2();
        System.out.println("--------------");
        Inter.show1();
        System.out.println("--------------");
        Inter.show2();
    }
}
posted @ 2020-06-06 20:07  赫兹x  阅读(156)  评论(0编辑  收藏  举报