Java筆記(09):面向對象--接口
阿新 • • 發佈:2017-05-21
案例 xtend mpc bsp args 筆記 cnblogs ani 同時
1、接口的成員特點:
1 /* 2 接口的特點: 3 A:接口用關鍵字interface表示 4 interface 接口名 {} 5 B:類實現接口用implements表示 6 class 類名 implements 接口名 {} 7 C:接口不能實例化 8 那麽,接口如何實例化呢? 9 按照多態的方式來實例化。 10 D:接口的子類 11 a:可以是抽象類。但是意義不大。 12b:可以是具體類。要重寫接口中的所有抽象方法。(推薦方案) 13 14 由此可見: 15 A:具體類多態(幾乎沒有) 16 B:抽象類多態(常用) 17 C:接口多態(最常用) 18 */ 19 //定義動物培訓接口 20 interface AnimalTrain { 21 public abstract void jump(); 22 } 23 24 //抽象類實現接口 25 abstract class Dog implements AnimalTrain { 26 } 27 28 //具體類實現接口29 class Cat implements AnimalTrain { 30 public void jump() { 31 System.out.println("貓可以跳高了"); 32 } 33 } 34 35 class InterfaceDemo { 36 public static void main(String[] args) { 37 //AnimalTrain是抽象的; 無法實例化 38 //AnimalTrain at = new AnimalTrain(); 39 //at.jump();40 41 AnimalTrain at = new Cat(); 42 at.jump(); 43 } 44 }
2、類與類、類與接口、接口與接口的關系:
1 /* 2 類與類: 3 繼承關系,只能單繼承,可以多層繼承。 4 類與接口: 5 實現關系,可以單實現,也可以多實現。 6 並且還可以在繼承一個類的同時實現多個接口。 7 接口與接口: 8 繼承關系,可以單繼承,也可以多繼承。 9 */ 10 interface Father { 11 public abstract void show(); 12 } 13 14 interface Mother { 15 public abstract void show2(); 16 } 17 18 interface Sister extends Father,Mother { 19 20 } 21 22 //class Son implements Father,Mother //多實現 23 class Son extends Object implements Father,Mother { 24 public void show() { 25 System.out.println("show son"); 26 } 27 28 public void show2() { 29 System.out.println("show2 son"); 30 } 31 } 32 33 class InterfaceDemo3 { 34 public static void main(String[] args) { 35 //創建對象 36 Father f = new Son(); 37 f.show(); 38 //f.show2(); //報錯 39 40 Mother m = new Son(); 41 //m.show(); //報錯 42 m.show2(); 43 } 44 }
3、抽象類和接口的區別
A:成員區別
抽象類:
成員變量:可以變量,也可以常量
構造方法:有
成員方法:可以抽象,也可以非抽象
接口:
成員變量:只可以常量
成員方法:只可以抽象
B:關系區別
類與類
繼承,單繼承
類與接口
實現,單實現,多實現
接口與接口
繼承,單繼承,多繼承
C:設計理念區別
抽象類 被繼承體現的是:”is a”的關系。抽象類中定義的是該繼承體系的共性功能。
接口 被實現體現的是:”like a”的關系。接口中定義的是該繼承體系的擴展功能。
4、貓狗案例:
1 /* 2 貓狗案例,加入跳高的額外功能 3 4 分析:從具體到抽象 5 貓: 6 姓名,年齡 7 吃飯,睡覺 8 狗: 9 姓名,年齡 10 吃飯,睡覺 11 12 由於有共性功能,所以,我們抽取出一個父類: 13 動物: 14 姓名,年齡 15 吃飯(); 16 睡覺(){} 17 18 貓:繼承自動物 19 狗:繼承自動物 20 21 跳高的額外功能是一個新的擴展功能,所以我們要定義一個接口 22 接口: 23 跳高 24 25 部分貓:實現跳高 26 部分狗:實現跳高 27 實現; 28 從抽象到具體 29 30 使用: 31 使用具體類 32 */ 33 //定義跳高接口 34 interface Jumpping { 35 //跳高功能 36 public abstract void jump(); 37 } 38 39 //定義抽象類 40 abstract class Animal { 41 //姓名 42 private String name; 43 //年齡 44 private int age; 45 46 public Animal() {} 47 48 public Animal(String name,int age) { 49 this.name = name; 50 this.age = age; 51 } 52 53 public String getName() { 54 return name; 55 } 56 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 public int getAge() { 62 return age; 63 } 64 65 public void setAge(int age) { 66 this.age = age; 67 } 68 69 //吃飯(); 70 public abstract void eat(); 71 72 //睡覺(){} 73 public void sleep() { 74 System.out.println("睡覺覺了"); 75 } 76 } 77 78 //具體貓類 79 class Cat extends Animal { 80 public Cat(){} 81 82 public Cat(String name,int age) { 83 super(name,age); 84 } 85 86 public void eat() { 87 System.out.println("貓吃魚"); 88 } 89 } 90 91 //具體狗類 92 class Dog extends Animal { 93 public Dog(){} 94 95 public Dog(String name,int age) { 96 super(name,age); 97 } 98 99 public void eat() { 100 System.out.println("狗吃肉"); 101 } 102 } 103 104 //有跳高功能的貓 105 class JumpCat extends Cat implements Jumpping { 106 public JumpCat() {} 107 108 public JumpCat(String name,int age) { 109 super(name,age); 110 } 111 112 public void jump() { 113 System.out.println("跳高貓"); 114 } 115 } 116 117 //有跳高功能的狗 118 class JumpDog extends Dog implements Jumpping { 119 public JumpDog() {} 120 121 public JumpDog(String name,int age) { 122 super(name,age); 123 } 124 125 public void jump() { 126 System.out.println("跳高狗"); 127 } 128 } 129 130 class InterfaceTest { 131 public static void main(String[] args) { 132 //定義跳高貓並測試 133 JumpCat jc = new JumpCat(); 134 jc.setName("哆啦A夢"); 135 jc.setAge(3); 136 System.out.println(jc.getName()+"---"+jc.getAge()); 137 jc.eat(); 138 jc.sleep(); 139 jc.jump(); 140 System.out.println("-----------------"); 141 142 JumpCat jc2 = new JumpCat("加菲貓",2); 143 System.out.println(jc2.getName()+"---"+jc2.getAge()); 144 jc2.eat(); 145 jc2.sleep(); 146 jc2.jump(); 147 148 } 149 }
Java筆記(09):面向對象--接口