接口java
一、多個無關的類能夠實現同一個接口this
二、一個類能夠實現多個無關的接口spa
三、與繼承關係相似,接口與實現類之間存在多態性code
四、定義java類的語法格式blog
< modifier> class < name>[extends< superclass>][implements< interface>[, interface]*]{繼承
<declarations>*接口
}內存
附:get
一、接口(interface)是抽象方法和常亮值的定義的結合。io
二、從本質上講,接口是一種特殊的抽象類,這種抽象類中只包含常亮和方法的定義,而沒有變量和方法的實現。
三、接口定義舉例:
public interface Runner{ public static final int id = 1; public void strrt(); public void run(); public void stop(); }
接口特性:
一、接口能夠實現多重繼承
二、接口聲明的屬性默認爲public static final 的;也只能是public static final 的;
三、接口中只能定義抽象方法,並且這些方法默認爲public 的、也只能是public的;
四、接口能夠繼承其餘接口,並添加新的屬性和抽象方法;
接口的使用以及接口實現多態:
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"); } }