Java多态实例主人和狗狗企鹅玩游戏

版权声明:Copyright 慕白博客 https://geekmubai.com https://blog.csdn.net/geekmubai/article/details/82556920

Java多态实例主人和狗狗企鹅玩游戏

需求说明:
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
提示:
Dog类添加catchingFlyDisc()方法,实现接飞盘功能
Penguin类添加swimming()方法,实现游泳功能
主人添加play(Pet pet)方法
如果pet代表Dog就玩接飞盘游戏
如果pet代表Penguin就玩游泳游戏

Pet类

public class Pet {
    private String name;
    protected int health=100;//健康值
    protected int love = 0;//亲密度

    public Pet() {
        super();
    }

    public Pet(String name, int health, int love) {
        super();
        this.name = name;
        this.health = health;
        this.love = love;

    }

//第二种写法:

/*
public abstract void play();
*/





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

Dog类:
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
Dog类添加catchingFlyDisc()方法,实现接飞盘功能

public class Dog extends Pet {
    private String strain;//品种


    public Dog() {
        super();
    }



    public Dog(String strain) {
        super();
        this.strain = strain;

    }

//第二种写法

/*
    @Override
    public void play() {
        // TODO Auto-generated method stub
        catchingFlyDisc(); //调用正在接飞盘的类
    }
*/


    public void catchingFlyDisc(){
        System.out.println("小狗正在接飞盘!");
        super.health =super.health-10; 
        System.out.println("健康值:" + super.health);
        super.love = super.love +5;
        System.out.println("亲密度:" + super.love);
    }

    public String getStrain() {
        return strain;
    }

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

Penguin类:
主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5 Penguin类添加swimming()方法,实现游泳功能

public class Penguin extends Pet {
    private String sex;


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

    }

//第二种写法

/*
@Override
    public void play() {
        // TODO Auto-generated method stub
        swimming();//调用正在游泳的类
    }
*/

    public void swimming(){
        System.out.println("企鹅正在接飞盘!");
        super.health =super.health-10; 
        System.out.println("健康值:" + super.health);
        super.love = super.love +5;
        System.out.println("亲密度:" + super.love);
    }

    public String getSex() {
        return sex;
    }


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

Master类:

public class Master {
    private String name;
    private int age;
    public Master() {
        super();
    }
    public Master(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public void play(Pet pet){
         if(pet instanceof Dog){
                Dog dog = (Dog)pet;
                dog.catchingFlyDisc();
            }else if(pet instanceof Penguin){
                Penguin p = (Penguin)pet;
                p.swimming();
            }

    }

//第二种写法

/*
public void play(Pet pet) {
        pet.play();
    }
*/



    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

测试类Test:

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Master m = new Master();

        m.play(new Dog());
        m.play(new Penguin());
    }
}

猜你喜欢

转载自blog.csdn.net/geekmubai/article/details/82556920