1. 接口新增特性
在jdk1.8以前接⼝中,只能有抽象方法,不能有任何方法的实现(方法体)。而在jdk1.8中打破常规,引⼊了新的关键字default,在接口中使⽤default修饰的方法,可以在接⼝⾥编写方法体。
1.1. 默认方法(default)
public interface Animal {
void eat();
default void sleep() {
System.out.println("睡觉");
}
}
实现了该接口的类,可以直接调用接口的默认方法。
1.2. 静态方法(static)
public interface Animal {
void eat();
static void run() {
System.out.println("跑起来");
}
}
可直接使用 接口名.静态方法
来调用接口中的静态方法。(如:Animal.run();)