java接口interface知識點實例總結

Java中在接口的應用中,要注意一下幾點:

<1>接口一般定義的是常量和一些抽象方法。抽象類中可以包含抽象方法,也可以有非抽象方法,但是有抽象方法的類一定是抽象類。抽象方法不能有方法體。

<2>在引用接口時,接口的引用指向實現的對象,儘量定義爲接口或父類的引用。這其中有可能用到多態的知識。引用接口用implements。

<3>接口(interface)只能定義抽象方法而且默認爲是Public。常量是public static final 修飾的

<4>通過implements來引用接口。例:Class runnrtmp inplements runner.

<5>多個無關類可以實現一個接口,!!!!接口的引用指向實現的對象。

<6>一個類可以實現多個無關的接口(這點和繼承要有所區別)

<7>和繼承一樣,接口與實現類之間存在多態性。

<8>接口可以繼承其他的接口,並添加新的屬性和抽象方法。

<9>在類中實現接口的方法時必須加上public修飾符

下面通過一個例子來對上面的要點進行下說明

接口的應用1:

複製代碼
 1 interface Runner //定義接口
 2 {
 3         int i=3;
 4         public void start();
 5         void run();
 6         void stop();
 7 }
 8 interface Eater extends Runner //接口間可以繼承
 9 {
10         public final static int j=4;
11         void openMouth();
12         void upAndDown();
13         void goIn();
14 }
15 class TT implements Eater //引用接口
16 {
17         public void start()
18         {
19                 System.out.println("---------start()-------");
20         }
21         public void run()
22         {
23                 System.out.println("---------run()-------");
24         }
25         public void stop()
26         {
27                 System.out.println("---------stop()-------");
28         }
29         public void openMouth()
30         {
31                 System.out.println("---------openMouth()-------");
32         }
33         public void upAndDown()
34         {
35                 System.out.println("---------upAndDown()-------");
36         }
37         public void goIn()
38         {
39                 System.out.println("---------goIn()-------");
40         }
41 }
42 public class TestInterface
43 {
44         public static void main(String[] args)
45         {
46                 Runner tt=new TT();//接口的引用指向實現的對象
47                 System.out.println(tt.i);
48                 System.out.println(Runner.i);
49                 tt.start();
50                 Eater ee=new TT();
51                 System.out.println(ee.j);
52                 System.out.println(Eater.j);
53                 ee.start();
54         }
55 }



console:

3
3
---------start()-------
4
4
---------start()-------


複製代碼

接口的應用2:

複製代碼
 1 public class TestInterface {
 2         
 3         public static void main(String[] args){
 4                 
 5                 CareAnimalable c = new Worker();
 6                 //Worker w = (Worker)c;
 7                 TestInterface t = new TestInterface();
 8                 t.t(c); //多態
 9 
10                 c = new Farmer();
11                 t.t(c); 
12 
13                 
14         }
15 
16         public void t(CareAnimalable c){//儘量定義爲接口或父類的引用
17                 c.feed();
18                 c.play();
19         }
20 }
21 
22 
23 interface CareAnimalable{
24         public void feed();
25         public void play();
26 }
27 
28 class Worker implements CareAnimalable{
29         
30         public void feed(){
31                 System.out.println("-----feed()----");
32         }
33 
34         public void play(){
35                 System.out.println("-----play()----");
36         }
37 }
38 
39 class Farmer implements CareAnimalable{
40         
41         public void feed(){
42                 System.out.println("-----Farmer feed()----");
43         }
44 
45         public void play(){
46                 System.out.println("-----Farmer play()----");
47         }
48 }
複製代碼

以上就是接口的用法,在編程開發中我們可以通過定義接口來簡便編程。在ANDROID開發中,接口的用處很大,所以希望大家能把接口的知識掌握。
發佈了35 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章