java之介面interface

高傑才_Android發表於2014-10-20

介面

1、多個無關的類可以實現同一個介面

2、一個類可以實現多個無關的介面

3、與繼承關係類似,介面與實現類之間存在多型性

4、定義java類的語法格式

< modifier> class < name>[extends< superclass>][implements< interface>[, interface]*]{

  <declarations>*

}

  

附:

1、介面(interface)是抽象方法和常亮值的定義的結合。

2、從本質上講,介面是一種特殊的抽象類,這種抽象類中只包含常亮和方法的定義,而沒有變數和方法的實現。

3、介面定義舉例:

public interface Runner{
    public static final int id = 1;
    public void strrt();
    public void run();
    public void stop();
}

 

 

介面特性:

1、介面可以實現多重繼承

2、介面宣告的屬性預設為public static final 的;也只能是public static final 的;

3、介面中只能定義抽象方法,而且這些方法預設為public 的、也只能是public的;

4、介面可以繼承其他介面,並新增新的屬性和抽象方法;

 

介面的使用以及介面實現多型:

public class TestInterface{
    public static void main(String args[]){
        Singer student = new Student("studentName");
        student.sing();
        student.sleep();
        
        Teacher teacher = new Teacher("TeacherName");
        teacher.painter();
        teacher.eat();
        
        Painter painter = (Painter)teacher;
        painter.painter();
        painter.eat();
        //下面是實現多型的部分
        TestInterface t1 = new TestInterface();
        t1.f(student);
        t1.f(teacher);
    }
    public void f(Singer s){
        s.sing();
    }
}
interface Singer{
    public void sing();
    public void sleep();
}
interface Painter{
    public void painter();
    public void eat();
}
class Student implements Singer{
    private String name;
    public Student(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void Study(){
        System.out.println("studying...");
    }
    public void sing(){
        System.out.println("study is singing");
    }
    public void sleep(){
        System.out.println("study is sleeping");
    }
}
class Teacher implements Singer,Painter{
    private String name;
    public Teacher(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void sing(){
        System.out.println("teacher is singing");
    }
    public void sleep(){
        System.out.println("teacher is sleeping");
    }
    public void painter(){
        System.out.println("teacher is paintering");
    }
    public void eat(){
        System.out.println("teacher is eating");
    }
}

執行結果:

 

記憶體分析圖:

 

 

示例:下面實現了一個三種不同的人給動物餵食和玩的多型,用介面實現。

public class TestDynamic{
    public static void main(String args[]){
        
        HelpAnimal farmer = new Farmer("farmer");
        HelpAnimal worker = new Worker("worker");
        HelpAnimal goverment = new Goverment("goverment");
        
        TestDynamic test = new TestDynamic();
        test.f(farmer);
        test.f(worker);
        test.f(goverment);
        
    }
    public void f(HelpAnimal animal){
            animal.play();
            animal.eat();
    }
}

interface HelpAnimal{
    public void eat();
    public void play();
}

class Farmer implements HelpAnimal{
    private String name;
    public Farmer(String name){
        this.name = name;
    }
    public void eat(){
        System.out.println("Farmer eat");
    }
    public void play(){
        System.out.println("Farmer play");
    }
}

class Worker    implements HelpAnimal{
    private String name;
    public Worker(String name){
        this.name = name;
    }
    public void eat(){
        System.out.println("Worker eat");
    }
    public void play(){
        System.out.println("Worker play");
    }
}

class Goverment    implements HelpAnimal{
    private String name;
    public Goverment(String name){
        this.name = name;
    }
    public void eat(){
        System.out.println("Goverment eat");
    }
    public void play(){
        System.out.println("Goverment play");
    }
}

 

相關文章