由於java的繼承支持單一繼承,不支持多繼承,所以在接口的使用在java中要多於繼承,現在介紹一個關於繼承和接口的綜合例題
請用代碼描述:
在動物園每一個動物都有顏色和年齡,都會吃
所有老虎都吃肉,但是有的老虎會表演(鑽火圈)
所有的大猩猩都吃香蕉,但有大猩猩會表演(騎自行車)
所有的鸚鵡都吃小米和說話,但是有的鸚鵡會表演(過蹺蹺板)
在動物園中管理員有年齡和姓名,他會餵動物和組織會表演的動物進行演出,如果演出時鸚鵡,還會讓鸚鵡說你好
操作步驟
/*
定義動物類(Animal)
屬性:顏色(color),年齡(age)
抽象方法: 吃(eat)
提供: 空參,帶參構造;setters和getters方法
*/
publicabstractclass Animal {
// 屬性:顏色(color),年齡(age)
private String color;
privateintage;
// 抽象方法: 吃(eat)
publicabstractvoid eat();
// 提供: 空參,帶參構造;setters和getters方法
public Animal() {
super();
}
public Animal(String color, intage) {
super();
this.color = color;
this.age = age;
}
public String getColor() {
returncolor;
}
publicvoid setColor(String color) {
this.color = color;
}
publicint getAge() {
returnage;
}
publicvoid setAge(intage) {
this.age = age;
}
}
/*
定義表演者接口(Actor)
抽象方法: play()
*/
publicinterface Actor {
// 表演
publicabstractvoid play();
}
/*
定義老虎類(Tiger),繼承動物類(Animal)
實現抽象方法: 吃(eat)
輸出格式: 2歲的黃色老虎吃肉
提供: 空參,帶參構造;
*/
publicclass Tiger extends Animal {
//實現抽象方法: 吃(eat)
// 輸出格式: 2歲的黃色老虎吃肉
publicvoid eat(){
System.out.println(getAge()+"歲的"+getColor()+"老虎吃肉");
}
//提供: 空參,帶參構造;
public Tiger() {
super();
}
public Tiger(String color, intage) {
super(color, age);
}
}
/*
定義會表演的老虎類(ActedTiger),繼承老虎類(Tiger),實現表演者接口
實現抽象方法表演(play)
輸出格式: 2歲的黃色老虎在表演鑽火圈
提供: 空參,帶參構造;
*/
publicclass ActedTiger extends Tiger implements Actor{
//實現抽象方法表演(play)
// 輸出格式: 2歲的黃色老虎在表演鑽火圈
publicvoid play(){
System.out.println(getAge()+"歲的"+getColor()+"老虎在表演鑽火圈");
}
//提供: 空參,帶參構造;
public ActedTiger() {
super();
}
public ActedTiger(String color, intage) {
super(color, age);
}
}
/*
定義大猩猩類(Gorilla),繼承動物類(Animal)
實現抽象方法: 吃(eat)
輸出格式: 3歲的黑色大猩猩吃香蕉
提供: 空參,帶參構造;
*/
publicclass Gorilla extends Animal {
//實現抽象方法: 吃(eat)
// 輸出格式: 3歲的黑色大猩猩吃香蕉
publicvoid eat(){
System.out.println(getAge()+"歲的"+getColor()+"大猩猩吃香蕉");
}
//提供: 空參,帶參構造;
public Gorilla() {
super();
}
public Gorilla(String color, intage) {
super(color, age);
}
}
/*
定義會表演的大猩猩(ActedGorilla),繼承大猩猩類(Gorilla),實現表演者接口
實現抽象方法表演(play)
輸出格式: 3歲的黑色大猩猩在表演騎自行車
提供: 空參,帶參構造;
*/
publicclass ActedGorilla extends Gorilla implements Actor {
//實現抽象方法表演(play)
// 輸出格式: 3歲的黑色大猩猩在表演騎自行車
publicvoid play(){
System.out.println(getAge()+"歲的"+getColor()+"大猩猩在表演騎自行車");
}
//提供: 空參,帶參構造;
public ActedGorilla() {
super();
}
public ActedGorilla(String color, intage) {
super(color, age);
}
}
/*
定義鸚鵡類(Parrot),繼承動物類(Animal)
實現抽象方法: 吃(eat)
輸出格式: 1歲的綠色鸚鵡在吃小米
特有方法: 說話(say)
輸出格式: 1歲的綠色鸚鵡在說你好
提供: 空參,帶參構造;
*/
publicclass Parrot extends Animal {
//實現抽象方法: 吃(eat)
// 輸出格式: 1歲的綠色鸚鵡在吃小米
publicvoid eat(){
System.out.println(getAge()+"歲的"+getColor()+"鸚鵡在吃小米");
}
//特有方法: 說話(say)
// 輸出格式: 1歲的綠色鸚鵡在說你好
publicvoid say(){
System.out.println(getAge()+"歲的"+getColor()+"鸚鵡在說你好");
}
//c)提供: 空參,帶參構造;
public Parrot() {
super();
}
public Parrot(String color, intage) {
super(color, age);
}
}
/*
定義會會表演的鸚鵡類(ActedParrot),繼承鸚鵡類(Parrot),實現表演者接口
實現抽象方法表演(play)
輸出格式: 1歲的綠色鸚鵡在表演過蹺蹺板
提供: 空參,帶參構造;
*/
publicclass ActedParrot extends Parrot implements Actor {
//實現抽象方法表演(play)
// 輸出格式: 1歲的綠色鸚鵡在表演過蹺蹺板
publicvoid play(){
System.out.println(getAge()+"歲的"+getColor()+"鸚鵡在表演過蹺蹺板");
}
//提供: 空參,帶參構造;
public ActedParrot() {
super();
}
public ActedParrot(String color, intage) {
super(color, age);
}
}
/*
定義動物園管理員類(ZooManager)
屬性:姓名(name),年齡(age)
行爲:
餵養動物(feed(Animal a)),調用吃的方法;
請思考此處爲什麼使用Animal作爲參數類型而不是其子類
在方法內部,調用a對象的eat方法
組織演出(letItShow(Actor a))
請思考此處爲什麼使用Actor接口類型作爲參數類型而不是其實現類
在方法內部
調用a的play方法
如果是鸚鵡,把a向下轉型爲Parrot類型,調用說話方法
提供空參,有參構造方法與setters和getters方法
*/
publicclass ZooManager {
// 屬性:姓名(name),年齡(age)
private String name;
privateintage;
// 行爲:
// 餵養動物(feed(Animal a)),調用吃的方法;
// 請思考此處爲什麼使用Animal作爲參數類型而不是其子類
// 在方法內部,調用a對象的eat方法
publicvoid feed(Animal a){
a.eat();
}
// 組織演出(letItShow(Actor a))
// 請思考此處爲什麼使用Actor接口類型作爲參數類型而不是其實現類
// 在方法內部
// 調用a的play方法
// 如果是鸚鵡,把a向下轉型爲Parrot類型,調用說話方法
publicvoid letItShow(Actor a){
// 調用a的play方法
a.play();
// 如果是鸚鵡,把a向下轉型爲Parrot類型,調用說話方法
if(ainstanceof Parrot){
Parrot p = (Parrot) a;
p.say();
}
}
// 提供空參,有參構造方法與setters和getters方法
public ZooManager() {
super();
}
public ZooManager(String name, intage) {
super();
this.name = name;
this.age = age;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(intage) {
this.age = age;
}
}
/*
定義測試類Test
提供main方法
在main方法
創建會表演的老虎類(ActedTiger)對象t,把顏色賦值爲黃色,年齡賦值爲2
創建會表演的大猩猩(ActedGorilla)對象g,把顏色賦值黑色,年齡賦值爲3
創建會表演的鸚鵡類(ActedParrot)對象 p,把顏色賦值爲綠色,年齡賦值1
創建物園管理員類(ZooManager)對象zm,把名稱賦值鄧超,年齡賦值30
調用zm對象的餵養動物(feed(Animala))方法,分別傳入t,g,p對象
調用zm對象的組織表演(letItShow(Actora))方法,分別傳入t,g,p對象
*/
publicclass Test {
publicstaticvoid main(String[] args) {
// 創建會表演的老虎類(ActedTiger)對象t,把顏色賦值爲黃色,年齡賦值爲2
ActedTiger t = new ActedTiger();
t.setAge(2);
t.setColor("黃色");
// 創建會表演的大猩猩(ActedGorilla)對象g,把顏色賦值黑色,年齡賦值爲3
ActedGorilla g = new ActedGorilla();
g.setAge(3);
g.setColor("黑色");
// 創建會表演的鸚鵡類(ActedParrot)對象 p,把顏色賦值爲綠色,年齡賦值1
ActedParrot p = new ActedParrot("綠色", 1);
// 創建物園管理員類(ZooManager)對象zm,把名稱賦值鄧超,年齡賦值30
ZooManager zm = new ZooManager("鄧超", 30);
// 調用zm對象的餵養動物(feed(Animala))方法,分別傳入t,g,p對象
zm.feed(t);
zm.feed(g);
zm.feed(p);
// 調用zm對象的組織表演(letItShow(Actora))方法,分別傳入t,g,p對象
zm.letItShow(t);
zm.letItShow(g);
zm.letItShow(p);
}
}