Java篇—关键字default的理解

default关键字介绍:

default是在java8中引入的关键字,也可称为Virtual extension methods——虚拟扩展方法。

它是指,在接口内部包含了一些默认的方法实现(也就是接口中可以包含方法体,这打破了Java之前版本对接口的语法限制),从而使得接口在进行扩展的时候,不会破坏与接口相关的实现类代码。

我们原来在写Java接口的时候,是不能有方法体的函数,就类似于C++中的虚函数,default关键字在接口中修饰方法时,方法可以有方法体。如下所示:

public interface Test {
    default public void method(){
        System.out.println("method belong to interface!");
    }
}

注:接口Test中的method方法在使用关键字default后,拥有了自身的方法体。

该特性的出现原因:

原来的接口是个双刃剑,好处是面向抽象而不是面向具体编程,缺陷是,当需要修改接口时候,需要修改全部实现该接口的类,目前的java8之前的集合框架没有foreach方法,通常能想到的解决办法是在JDK里给相关的接口添加新的方法及实现。然而,对于已经发布的版本,是没法在给接口添加新方法的同时不影响已有的实现。所以引进的默认方法。他们的目的是为了解决接口的修改与现有的实现不兼容的问题。

如何使用default关键字?

(1)调用父接口实现

创建接口TestDemo1,并且在接口TestDemo1中定义默认方法helloWorld()

public interface TestDemo1{
    default void helloWorld() {
        System.out.println("helloWorld belong to TestDemo1");
    }
}

这时可以编写一个类MyTestDemo实现接口TestDemo1,并调用接口中定义的默认方法helloWorld()

public class MyTestDemo implements TestDemo1{
    public static void main(String[] args) {
        MyTestDemo myTestDemo = new MyTestDemo();
        //直接调用helloWorld()方法
        myTestDemo.helloWorld();    //输出结果为:helloWorld belong to TestDemo1
    }
}

(2)同时继承两个接口

创建接口TestDemo2,并且在接口TestDemo2中定义默认方法helloWorld()

public interface TestDemo2{
    default void helloWorld() {
        System.out.println("helloWorld belong to TestDemo2");
    }
}

这时编写一个类MyTestDemo同时实现TestDemo1和TestDemo2

public class MyTestDemo implements TestDemo1,TestDemo2{
    public static void main(String[] args) {
        MyTestDemo myTestDemo = new MyTestDemo();
        //直接调用helloWorld()方法
        myTestDemo.helloWorld();
    }
}

运行后,会发现编译器报错,报错信息如下:

发生这种情况的原因是,实现类MyTestDemo即实现了接口TestDemo1又实现了接口TestDemo2,恰巧两个接口中都定义可相同的默认方法。说白了就是编译器此时已经懵逼了,当我们在MyTestDemo类中调用方法时,它不知道该去调用TestDemo1的默认方法还是去调用TestDemo2的方法。

解决方法就是在实现MyTestDemo类中实现该方法,代码如下:

public class MyTestDemo implements TestDemo1,TestDemo2{
    @Override
    public void helloWorld(){
        System.out.println("helloWorld belong to MyTestDemo");
    }
    public static void main(String[] args) {
        MyTestDemo myTestDemo = new MyTestDemo();
        myTestDemo.helloWorld();   //运行结果: helloWorld belong to MyTestDemo
    }
}

(3)类优先于接口

此时创建一个实现类MyTestDemo2,该实现类不仅继承了MyTestDemo并且实现了TestDemo2 

public class MyTestDemo2 extends MyTestDemo implements TestDemo2 {
    public static void main(String[] args) {
        MyTestDemo2 myTestDemo2 = new MyTestDemo2();
        myTestDemo2.helloWorld(); 
    }
}

思考:在实现类MyTestDemo2中调用helloWorld()方法,到底执行的是MyTestDemo中的方法还是执行TestDemo2 中的方法?

输出结果如下:

Java篇—关键字default的理解_第1张图片

结论:类优先于接口,所以将会执行MyTestDemo中的方法。

你可能感兴趣的:(学习历程)