Programming to realize “electronic pet mall”

*/

The code is as follows:

//Test class

Public class demo {public static void main (string [] args) {dog H = new dog ("H", 60, "Chinese pastoral dog"); Penguin QQ = New Penguin ("Q", 80, gender. Q); master master = new master(); H. showinfo(); master. Feeddog (h); H. showinfo(); QQ. Showinfo(); master. Feedpenguin (QQ); QQ. Showinfo();}

}

//Dog class

Public class dog {string name; int health; / / health value string variable; / / variety / / parameterless constructor public dog() {} / / parameterless constructor public dog (string name, int health, string variable) {super(); this. Name = name; this. Health = health; this. Variable = variable;} Public void showinfo() {system.out.println ("dog information -----------"); system.out.println ("Name:" + name); system.out.println ("health value:" + Health); system.out.println ("variety:" + variable);} Public void eat() {system. Out. Println (name + "eating meat"); health + = 5;}

}

//Penguin class

public class Penguin { String name; int health; gender gender; public Penguin() { } public Penguin(String name,text5.gender gender) { super(); this.name = name; this.health = health; this.gender = gender; } Public void showinfo() {system. Out. Println ("Penguin information -----------"); system. Out. Println ("nickname:" + name); system. Out. Println ("health value:" + Health); system. Out. Println ("gender:" + gender);} Public void eat() {system. Out. Println (name + "eating fish"); health + = 5;}

}

/** define enumeration type: gender * variables defined by gender can only take values: q-child, q-sister * / public enum gender {q-child, q-sister

}

//Master class

public class Master { String name; public void FeedDog(Dog dog){ dog.eat(); } public void FeedPenguin(Penguin penguin){ penguin.eat(); } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>