//Animal的两个子类
public class Dog extends Animal{
private String breed;
public Dog(String name, int health, String breed, int love) {
super(name,health,love);
setBreed(breed);
}
public void shout() {
super.shout();
System.out.println("我的品种是" + breed);
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
}
public class Penguin extends Animal {
private String sex;
public Penguin(String name, int health, String sex, int love) {
super(name, health, love);
setSex(sex);
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void shout() {
super.shout();
System.out.println("我的性别是" + sex);
}
}
//主人
public class Master {
public void cure(Dog dog) {
// 如果狗的健康状况不佳
if (dog.getHealth() < 50) {
System.out.println("去医院看病");
System.out.println("打针");
System.out.println("吃药");
// 治疗后,狗的健康状况变为60
dog.setHealth(60);
}
}
public void cure(Penguin penguin) {
// 如果企鹅的健康状况不佳
if (penguin.getHealth() < 50) {
System.out.println("去医院看病");
System.out.println("吃药");
System.out.println("疗养");
// 治疗后,企鹅的健康状况变为70
penguin.setHealth(70);
}
}
}
//宠物商店
import java.io.IOException;
import java.util.Scanner;
public class PetShop {
public static void main(String[] args) {
Dog dog=null;
Penguin penguin=null;
System.out.println("欢迎来到宠物商店");
System.out.println("请输入要领养的宠物的名字");
Scanner input = new Scanner(System.in);
String name = input.next();
System.out.println("请输入你要领养的宠物的类型:(1 狗狗 2 企鹅)");
int type = input.nextInt();
switch (type) {
case 1:
System.out.println("请输入狗的品种(1 聪明的拉布拉多犬 2 酷酷的雪纳瑞)");
int breed = input.nextInt();
System.out.println("请输入狗的健康值");
int health = input.nextInt();
String s = "";
if (breed == 1) {
s = "聪明的拉布拉多犬";
} else {
s = "酷酷的雪纳瑞";
}
dog = new Dog(name, health, s, 20);
dog.shout();
break;
case 2:
System.out.println("请输入企鹅的性别(1Q仔 2Q妹)");
int sex = input.nextInt();
System.out.println("请输入企鹅的健康值");
health = input.nextInt();
s = "";
if (sex == 1) {
s = "Q仔";
} else {
s = "Q妹";
}
penguin = new Penguin(name, health, s, 30);
penguin.shout();
}
//创建主人
Master master=new Master();
master.cure(dog);
dog.shout();
master.cure(penguin);
penguin.shout();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
希望可以关注我一下,我是个初学者,请多多指教!