public class Animal {
private String name;
private int health;
private int love;
public Animal() {
super();
}
public Animal(String name, int health, int love) {
super();
this.name = name;
this.health = health;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void lookDoctor(){
System.out.println("我是Animal类中看病的lookDoctor()方法");
}
public class Cat extends Animal {
private String color;
public Cat() {
super();
}
public Cat(String name, int health, int love, String color) {
super(name, health, love);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void lookDoctor() {
System.out.println("猫生病了,打针和吃药.....");
this.setHealth(85);
}
public class Dog extends Animal {
private String strain;
public Dog() {
super();
}
public Dog(String name, int health, int love, String strain) {
super(name, health, love);
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
@Override
public void lookDoctor() {
System.out.println("狗生病了,打针.......");
this.setHealth(70);
}
public class Penguin extends Animal {
private char sex;
public Penguin() {
super();
}
public Penguin(String name, int health, int love, char sex) {
super(name, health, love);
this.sex = sex;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Override
public void lookDoctor() {
System.out.println("企鹅生病了,吃药.......");
this.setHealth(75);
}
只要重写lookDoctor()方法,再创个主人类(不需要在主人类添加新的宠物和方法)public class Master {
public void cure(Animal animal){
if(animal.getHealth()<60){
animal.lookDoctor();
}
}
public class Test {
public static void main(String[] args) {
Master master = new Master();
Dog dog1 = new Dog("旺财", 30, 99, "藏獒");
System.out.println(dog1.getHealth());
master.cure(dog1);
System.out.println(dog1.getHealth());
System.out.println("----------------");
Penguin penguin1 = new Penguin("QQ", 45, 90, '母');
System.out.println(penguin1.getHealth());
master.cure(penguin1);
System.out.println(penguin1.getHealth());
System.out.println("----------------");
Animal animal = new Dog("来福", 20, 100, "拉布拉多");
System.out.println(animal.getHealth());
master.cure(animal);
System.out.println(animal.getHealth());
System.out.println("---------------------------------");
animal = new Penguin("QQ", 50, 92, '公');
System.out.println(animal.getHealth());
master.cure(animal);
System.out.println(animal.getHealth());
}
运行结果相关知识
成都哪里有给鹦鹉看病的机构成都哪里有给鸟八哥看病的?
大渡口哪个宠物医院可以给乌龟看病?
北京房山区哪有给小鸭子看病的地方
给狗看病比人贵,宠物医院的水有多深?
注意给你家狗狗看病的宠物店,有资质么?
给宠物看病动辄花上万元,如何破解?
给猫看病比人都贵,钱到底让谁赚了
大连哪家宠物医院能给乌龟看病?
一通检查上千元,给宠物看病如何才能明明白白?
猫狗看病动辄花几千元 宠物医疗为何如此昂贵
网址: 多态的使用给小动物看病 https://m.mcbbbk.com/newsview23970.html
上一篇: [附源码]宠物领养管理系统696 |
下一篇: 【小技巧】宠物健康信息值:如何辨 |