您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么用java多態實現電子寵物系統的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
1、主人類:
public class Master { //領養 public Pet adopt(String name){ if ("狗狗".equals(name)){ return new Dog(); }else if (name.equals("企鵝")){ return new Penguin(); } return null; } //喂食 public void feed(Pet pet, String food){ pet.eat(food); } //玩耍 public void play(Pet pet){ if(pet instanceof Dog){ Dog d= (Dog) pet; d.catchDisk(); }else if(pet instanceof Penguin){ Penguin p= (Penguin) pet; p.swimInSouth(); } } //捐獻 public Pet donate(String name){ if("狗狗".equals(name)){ return new Dog(); }else if(name.equals("企鵝")){ return new Penguin(); } return null; } }
2、狗狗類:
public class Dog extends Pet { public void catchDisk(){ System.out.println("狗狗接飛盤!"); } public Dog(int health) { super(health); } public Dog() { } @Override public void eat(String food) { System.out.println("狗狗吃"+food+",吃飽后健康值+3"); setHealth(getHealth()+3); } }
3、企鵝類:
public class Penguin extends Pet{ public void swimInSouth(){ System.out.println("企鵝在南極游泳!"); } public Penguin(int health) { super(health); } public Penguin() { } @Override public void eat(String food) { System.out.println("企鵝寶寶吃"+food+",吃完后,健康值加5"); setHealth(getHealth()+5); } }
4、寵物類,Pet:
public class Pet { private int health; public int getHealth() { return health; } public void setHealth(int health) { this.health = health; if(this.health>100){ this.health=100; System.out.println("已經吃飽了,再吃就撐破了"); } } public Pet() { } public Pet(int health) { this.health = health; } public void eat(String food){ System.out.println("寵物吃"+food); } }
5、測試類:
public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Master master = new Master(); //領養 System.out.print("請輸入您要領養的寵物:"); String dname = sc.next(); Pet p = master.adopt(dname); //主人給狗狗喂食,玩耍 System.out.print("請輸入您要喂得食物:"); String dfood = sc.next(); master.feed(p,dfood); System.out.println(p.getHealth()); master.play(p); //主人給企鵝喂食,玩耍 System.out.print("請輸入您要玩的寵物:"); String pname = sc.next(); p = master.adopt(pname); System.out.print("請輸入您要喂的食物:"); String pfood = sc.next(); master.feed(p,pfood); System.out.println(p.getHealth()); master.play(p); //捐贈出去 System.out.print("請輸入您要捐贈的寵物:"); String jname = sc.next(); Pet pet = master.donate(jname); System.out.println(); System.out.println("您捐獻的是:"+pet.getClass().getName()); } }
補:這里測試類我是讓用戶在控制臺輸入,如果不想輸入,可將測試類Test換成下面的代碼:
public class TestPet { public static void main(String[] args) { Master m = new Master(); //領養 Pet p = m.adopt("狗狗"); //主人給狗狗喂食,玩耍 m.feed(p,"大骨頭"); System.out.println(p.getHealth()); m.play(p); //主人給企鵝喂食,玩耍 p = m.adopt("企鵝"); m.feed(p,"小魚"); System.out.println(p.getHealth()); m.play(p); //捐獻出去 Pet pet = m.donate("狗狗"); System.out.println("您捐獻的是:"+pet.getClass().getName()); } }
以上就是“怎么用java多態實現電子寵物系統”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。