学习内容:接口的基本概念

1.接口的基本概念

1、定义一个接口,使用interface关键字如:

 

interface IEat{

}

 

 在一个接口中,只能定义常量、抽象方法

 

interface IEat{
       public void eat(); //方法
       public void int NUM = 10;  //常量
        
}

 

接口可以继承多个接口

 


public class Test{
       public static void main(String []args){

    } 

}


interface IEat{
    void eat();
    void int NUM = 10;
}

interface IRun{
    void run();      
} 

interface ISleep extends IEat,IRun{      //Sleep 接口继承其他两个接口
     void sleep();   
}

//具体类实现接口必须实现接口的所有方法,使用implements关键字
class Man implements ISleep{
    private String name;
    public Man(){}  //保留默认构造方法
    public Man(String name){
        this.name = name;
    }
        public void sleep() {
        System.out.println("我爱睡觉");
    }
    public void eat() {
        System.out.println("我是"+name+",我爱吃苹果");
    }
    public void run() {
        System.out.println("吃完就跑");
    }
}
   

在接口中定义的方法没有声明访问修饰符,默认为public

interface IEat{
    void eat();
    void int NUM = 10;   
}

2  面向对象的多态性:

 

 

 

 

一个具体类实现接口使用implements关键字( 实现接口必须要实现接口的所有方法 )

 

posted @ 2020-05-11 13:44  老智障  阅读(200)  评论(0编辑  收藏  举报