多态在宠物系统中的应用

多态:同一个引用类型,使用不同的实例而执行不同的操作

使用多态的两种形式:

1.使用父类作为方法形参实现多态

2.使用父类作为方法返回值实现多态

1.主人给宠物喂食功能

不同宠物吃的东西不同
主人可以喂养不同类型宠物

2.主人与宠物玩耍功能

主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5;主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5

宠物抽象类;Pet

package Animal;

/**
 * 宠物类Pet
 * 
 * @author Administrator
 *
 */
public abstract class Pet {
    protected String name;
    protected int health;
    protected int love;

    public Pet() {

    }

    public void print() {
        System.out.println("宠物的自白:\n我的名字叫 " + this.name + ",健康值 " + this.health + "和主人的亲密度" + this.love + "。");
    }

    public abstract void eat();

    public Pet(String name) {
        this.name = name;
    }

    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;
    }
}
View Code

狗类Dog

扫描二维码关注公众号,回复: 5751690 查看本文章
package Animal;
/**
 * 狗类Dog
 * @author Administrator
 *
 */
public class Dog extends Pet{
private String strain;
public Dog(String name,String strain){
    super(name);//构造
    this.strain=strain;
}
public void print(){
    super.print();//调用父类print方法
    System.out.println("我是一只 "+this.strain+", ");
} 
public void eat(){
    super.health=super.health+3;
    System.out.println("狗狗"+super.name+"吃饱了!健康值增加3");
}

//实现接飞盘功能
public void catchingFlydisc(){
    System.out.println("狗狗"+super.name+"正在接飞盘。");
    super.health+=10;
    super.love+=5;
}

public String getStrain() {
    return strain;
}
public void setStrain(String strain) {
    this.strain = strain;
}


}
View Code

企鹅类:Penguin

package Animal;

/**
 * 企鹅Penguin类
 * 
 * @author Administrator
 *
 */
public class Penguin extends Pet {
    private String sex;

    public Penguin(String name, String sex) {
        super(name);
        this.sex = sex;
    }

    public void print() {
        super.print();
        System.out.println("性别是" + this.sex + ",");
    }

    public void eat() {
        super.health += 5;
        System.out.println("企鹅" + super.name + "吃饱了,健康值增加5");
    }

    // 实现游泳功能
    public void swimming() {
        System.out.println("企鹅" + super.name + "正在游泳。");
        super.health -= 10;
        super.love += 5;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}
View Code

主人类:Master

package Animal;

/**
 * Master主人类
 * 
 * @author Administrator
 *
 */
public class Master {
    private String name;
    private int money = 0;

    public Master(String name, int money) {
        this.name = name;
        this.money = money;
    }

    // 喂食功能
    public void feed(Pet pet) {
        pet.eat();
    }

    // 玩耍功能
    public void play(Pet pet) {//
        if (pet instanceof Dog) {
            Dog dog = (Dog) pet;// 强制转换
            dog.catchingFlydisc();
        } else if (pet instanceof Penguin) {
            Penguin pen = (Penguin) pet;
            pen.swimming();
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

}
View Code

测试Test类

package Animal;
/**
 * 实现调试功能
 * @author Administrator
 *
 */
public class Test {

    public static void main(String[] args) {
Dog dog=new Dog("欧欧","雪纳瑞");
Penguin pen=new Penguin("楠楠","Q妹妹");
Master master =new Master("王先生",100);
master.feed(dog);
master.feed(pen);
master.play(pen);

    }

}
View Code

      

猜你喜欢

转载自www.cnblogs.com/helloworld2019/p/10643434.html