接口

jdk1.8之前的接口

  • jdk1.8之前不能存在静态的方法,全是抽象的方法,不能有方法体存在。
public interface NewInterface {
   public void run();
}

jdk1.8的接口

  • jdk1.8 接口可以有静态的方法,静态方法不能被重写,可以通过 接口.方法名() 进行访问。
public interface NewInterface {
   public static void run(){
       System.out.println("我在跑步");
   }
}
public class Student  {

    public static void main(String[] args) {
        NewInterface.run();
    }
}
  • jdk1.8接口可以有非静态的方法。但是要被default修饰。如果实现类实现了多个接口,且接口存在同名同参的方法,编译器必须要实现类重写同名同参的方法,因为实现类不知道继承那个接口的方法,重写的方法不能用default修饰,default只能修饰接口中的方法。
public interface newStudent {
    default void run(){
       System.out.println("新的学生");
   }
    static void go(){
       System.out.println("我在走");
   }
}
public interface NewInterface {
    default  void run() {
        System.out.println("我在跑步");
    }
}
public class Student  implements NewInterface,newStudent{

    @Override
    public void run() {
        System.out.println("子类的方法");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值