默认方法

默认方法是JDK8新特性,指的是接口也可以提供具体方法了,而不像以前,只能提供抽象方法

Mortal 这个接口,增加了一个默认方法 revive,这个方法有实现体,并且被声明为了default

好处是当要改变这个接口中的某一方法时,不再需要改变每一个,只需要在定义接口时实现

package charactor;
 
public interface Mortal {
    public void die();
 
    default public void revive() {
        System.out.println("本英雄复活了");
    }
}

猜你喜欢

转载自blog.csdn.net/Whiteleaf3er/article/details/82019876