Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java Polymorphism to realize Electronic Pet system

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to use java polymorphism to achieve electronic pet system related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's learn about it.

The effect is achieved:

Code implementation:

1. Lord Human beings:

Public class Master {/ / adopt public Pet adopt (String name) {if ("dog" .equals (name)) {return new Dog ();} else if (name.equals ("penguin")) {return new Penguin ();} return null;} / / feed public void feed (Pet pet, String food) {pet.eat (food) / / play 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 () } / / donate public Pet donate (String name) {if ("dog" .equals (name)) {return new Dog ();} else if (name.equals ("penguin")) {return new Penguin ();} return null;}}

2. Dogs:

Public class Dog extends Pet {public void catchDisk () {System.out.println ("Dogs catch Frisbee!") ;} public Dog (int health) {super (health);} public Dog () {} @ Override public void eat (String food) {System.out.println ("dog eat" + food+ ", health value + 3"); setHealth (getHealth () + 3);}}

3. Penguins:

Public class Penguin extends Pet {public void swimInSouth () {System.out.println ("Penguins swim in Antarctica!") ;} public Penguin (int health) {super (health);} public Penguin () {} @ Override public void eat (String food) {System.out.println ("baby penguins eat" + food+ ", after eating, the health value adds 5"); setHealth (getHealth () + 5);}}

4. Pet category, 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 ("pet eat" + food);}}

5. Test class:

Public class Test {public static void main (String [] args) {Scanner sc = new Scanner (System.in); Master master = new Master (); / / adopt System.out.print ("Please enter the pet you want to adopt:"); String dname = sc.next (); Pet p = master.adopt (dname) / / the owner feeds the dog and plays with System.out.print ("Please enter the food you want to feed:"); String dfood = sc.next (); master.feed (pdfood); System.out.println (p.getHealth ()); master.play (p) / / the owner feeds the penguin and plays with System.out.print ("Please enter the pet you want to play with:"); String pname = sc.next (); p = master.adopt (pname); System.out.print ("Please enter the food you want to feed:"); String pfood = sc.next (); master.feed (pforce pfood) System.out.println (p.getHealth ()); master.play (p); / / donated System.out.print ("Please enter the pet you want to donate:"); String jname = sc.next (); Pet pet = master.donate (jname); System.out.println (); System.out.println ("you donated:" + pet.getClass (). GetName ()) }}

Supplement: here I ask the user to enter the test class in the console. If you don't want to enter it, you can replace the test class Test with the following code:

Public class TestPet {public static void main (String [] args) {Master m = new Master (); / adopt Pet p = m.adopt ("dog"); / / the owner feeds the dog and plays with m.feed (p, "big bones"); System.out.println (p.getHealth ()); m.play (p) / / owners feed penguins and play p = m.adopt ("penguins"); m.feed (p, "little fish"); System.out.println (p.getHealth ()); m.play (p); / / donate Pet pet = m.donate ("dog"); System.out.println ("you donated:" + pet.getClass (). GetName ()) }} these are all the contents of the article "how to use java Polymorphism to implement Electronic Pet system". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report