接口
接口(interface)用来描述类应该做什么,而不是指定具体应该如何做。
- 接口不是类,不能使用new实例化接口对象。(必须引用实现这个接口的类)
interface Comparable{ //Compareable声明为接口类型
public Compareable();
}
× Compareable test = new Compareable(); //改写法无法运行,会报错
√ Test implements Compareable{
public Compareable(){};
}
Compareable test = new Test ();
- 默认方法
可以为接口方法提供一个默认实现,必须使用default修饰符标记这样的方法。
public interface Comparable<T>
default int comparable(T other ){return 0};