一、JDK1.8接口新增
1)默认方法
即被default关键字修饰的方法
public class Demo01 {
public static void main(String[] args) {
A a=new Impl();
a.haha();
}
}
//接口
interface A{
public default void dsiplay(){
System.out.println("我是默认方法");
}
}
//实现类
class Demo implements A{
// public default void dsiplay(){ //报错 因为只有接口中才能存在默认方法
// System.out.println("我是重写后的默认方法");
// }
//构成重写
public void dsiplay(){
System.out.println("我是重写后的默认方法");
}
}
使用:通过实现类对象使用,实现类可以对默认方法进行重写,调用重写后的方法
注:只有接口能够使用default关键字,重写时不可以使用default关键字
2)静态方法
使用:接口中的静态方法,只能通过接口名.调用,不能通过实现类对象调用
public class Demo01 {
public static void main(String[] args) {
A.hehe();
}
}
//接口
interface A{
public static void hehe(){
System.out.println("我是接口中的静态方法");
}
}