转自:http://www.cnblogs.com/zxl-jay/archive/2011/09/24/2189740.html
有所修改
Java中在接口的应用中,要注意一下几点:
<1>接口一般定义的是常量和一些抽象方法。抽象类中可以包含抽象方法,也可以有非抽象方法,但是有抽象方法的类一定是抽象类。抽象方法不能有方法体。
<2>在引用接口时,接口的引用指向实现的对象,尽量定义为接口或父类的引用。这其中有可能用到多态的知识。引用接口用implements。
<3>接口(interface)只能定义抽象方法而且默认为是Public。常量是public static final 修饰的
<4>通过implements来引用接口。例:Class runnrtmp inplements runner.
<5>多个无关类可以实现一个接口,!!!!接口的引用指向实现的对象。
<6>一个类可以实现多个无关的接口(这点和继承要有所区别)
<7>和继承一样,接口与实现类之间存在多态性。
<8>接口可以继承其他的接口,并添加新的属性和抽象方法。
<9>在类中实现接口的方法时必须加上public修饰符
下面通过一个例子来对上面的要点进行下说明
接口的应用1:
View Code
1 1 interface Runner //定义接口 2 2 { 3 3 int i=3; 4 4 public void start(); 5 5 void run(); 6 6 void stop(); 7 7 } 8 8 interface Eater extends Runner //接口间可以继承 9 9 { 10 10 public final static int j=4; 11 11 void openMouth(); 12 12 void upAndDown(); 13 13 void goIn(); 14 14 } 15 15 class TT implements Eater //引用接口 16 16 { 17 17 public void start() 18 18 { 19 19 System.out.println("---------start()-------"); 20 20 } 21 21 public void run() 22 22 { 23 23 System.out.println("---------run()-------"); 24 24 } 25 25 public void stop() 26 26 { 27 27 System.out.println("---------stop()-------"); 28 28 } 29 29 public void openMouth() 30 30 { 31 31 System.out.println("---------openMouth()-------"); 32 32 } 33 33 public void upAndDown() 34 34 { 35 35 System.out.println("---------upAndDown()-------"); 36 36 } 37 37 public void goIn() 38 38 { 39 39 System.out.println("---------goIn()-------"); 40 40 } 41 41 } 42 42 public class TestInterface 43 43 { 44 44 public static void main(String[] args) 45 45 { 46 46 Runner tt=new TT();//接口的引用指向实现的对象 47 47 System.out.println(tt.i); 48 48 System.out.println(Runner.i); 49 49 tt.start(); 50 50 Eater ee=new TT(); 51 51 System.out.println(ee.j); 52 52 System.out.println(Eater.j); 53 53 ee.start(); 54 54 } 55 55 }
接口的应用2:
View Code
1 1 public class TestInterface { 2 2 3 3 public static void main(String[] args){ 4 4 5 5 CareAnimalable c = new Worker(); 6 6 //Worker w = (Worker)c; 7 7 TestInterface t = new TestInterface(); 8 8 t.t(c); //多态 9 9 10 10 c = new Farmer(); 11 11 t.t(c); 12 12 13 13 14 14 } 15 15 16 16 public void t(CareAnimalable c){//尽量定义为接口或父类的引用 17 17 c.feed(); 18 18 c.play(); 19 19 } 20 20 } 21 21 22 22 23 23 interface CareAnimalable{ 24 24 public void feed(); 25 25 public void play(); 26 26 } 27 27 28 28 class Worker implements CareAnimalable{ 29 29 30 30 public void feed(){ 31 31 System.out.println("-----feed()----"); 32 32 } 33 33 34 34 public void play(){ 35 35 System.out.println("-----play()----"); 36 36 } 37 37 } 38 38 39 39 class Farmer implements CareAnimalable{ 40 40 41 41 public void feed(){ 42 42 System.out.println("-----Farmer feed()----"); 43 43 } 44 44 45 45 public void play(){ 46 46 System.out.println("-----Farmer play()----"); 47 47 } 48 48 }
接口的应用3:
View Code
1 interface PCI 2 { 3 public void use(); 4 public void close(); 5 } 6 class NetCard implements PCI 7 { 8 public void use() 9 { 10 System.out.println("run NetCard"); 11 } 12 public void close() 13 { 14 System.out.println("close NetCard"); 15 } 16 } 17 class MainBoard 18 { 19 public void run() 20 { 21 System.out.println("run MainBoard"); 22 } 23 public void usePCI(PCI p) 24 { 25 p.use(); 26 p.close(); 27 } 28 } 29 public class TestMainBoard 30 { 31 public static void main(String[] args) 32 { 33 MainBoard mb = new MainBoard(); 34 mb.run(); 35 mb.usePCI(new NetCard()); 36 } 37 }
注意:
接口定义中,数据和方法的默认定义方式为:
数据:public static final
方法: public abstract
在类中,重写接口中的方法时,务必加上修饰符public
例子:
interface InterA { public static final int OKA = 1; public abstract void showA(); } interface InterB { public abstract void showB(); } interface InterC extends InterA,InterB//JAVA可支持接口的多继承,但不支持类的多继承 { public abstract void showC(); } interface InterD { public abstract void showD(); } class TextInter implements InterC,InterD//JAVA可实现接口的多实现 { public void showA() { System.out.println("showA"); } public void showB() { System.out.println("showB"); } public void showC() { System.out.println("showC"); } public void showD() { System.out.println("showD"); } } public class TestInterface { public static void main(String[] args) { TextInter textInter = new TextInter(); textInter.showA(); textInter.showB(); textInter.showC(); textInter.showD(); System.out.println(textInter.OKA); System.out.println(TextInter.OKA); System.out.println(InterA.OKA); } }