2019年1月21日 面向对象——>多态

java源文件分享地址:

链接:https://pan.baidu.com/s/15ogV0FmpWRLBPbiqYeVEfg
提取码:fwon
复制这段内容后打开百度网盘手机App,操作更方便哦

作业一:

创建宠物父类:编写代码如下

package com.zuoye2;

public abstract class Pet {
    private String name;
    private int love;
    private int health;

    public Pet() {
    }

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

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public int getLove() {

        return love;
    }

    public void setLove(int love) {
        if(love<0||love>100){
            System.out.println("健康值有误,取默认值。。。");
            love = 60;
        }

        this.love = love;
    }

    public int getHealth() {

        return health;
    }

    public void setHealth(int health) {
        if(health<0||health>100){
            System.out.println("健康值有误,取默认值。。。");
            health = 60;
        }
        this.health = health;
    }
    public abstract void showInfo();
    public abstract void toEat();

}

创建狗狗子类:编写代码如下

package com.zuoye2;

public class Dog extends Pet {
    public Dog() {
    }

    public Dog(String name, int love, int health) {
        super(name, love, health);
    }

    public void showInfo(){
        System.out.println("我叫"+this.getName()+",和主人的亲密度是:"+this.getLove()+",我的健康值是:"+this.getHealth());
    }

    public void toEat(){
        if(getHealth()<100){
            System.out.println("给狗狗喂食狗粮。。。。。。。。。。。");
            setHealth(getHealth()+3);
        }else {
            System.out.println("健康值达到100时,不需要喂食。。。。");
        }

    }

}

创建企鹅子类:编写代码如下

package com.zuoye2;

public class Penguin extends Pet {
    public Penguin() {
    }

    public Penguin(String name, int love, int health) {
        super(name, love, health);
    }

    public void showInfo(){
        System.out.println("我叫"+this.getName()+",和主人的亲密度是:"+this.getLove()+",我的健康值是:"+this.getHealth());
    }

    public void toEat(){
        if(getHealth()<100){
            System.out.println("给企鹅喂食小鱼。。。。。。。。。。。");
            setHealth(getHealth()+5);
        }else {
            System.out.println("健康值达到100时,不需要喂食。。。。");
        }

    }
}

创建主人类:

package com.zuoye2;

public class Master {
    public void feed(Pet pet){
        pet.toEat();
    }
}

创建测试类:

package com.zuoye2;

public class Test {
    public static void main(String[] args) {
        Master m = new Master();
        Pet dog = new Dog("豆豆",50,50);
        m.feed(dog);
        dog.showInfo();

        Pet pen = new Penguin("Q仔",70,100);
        m.feed(pen);
        pen.showInfo();
    }
}

 

输出结果为:

给狗狗喂食狗粮。。。。。。。。。。。
我叫豆豆,和主人的亲密度是:50,我的健康值是:53
健康值达到100时,不需要喂食。。。。
我叫Q仔,和主人的亲密度是:70,我的健康值是:100

 

作业二:

创建宠物父类:

package com.zuoye1;

public abstract class Pet {
    private String name;
    private int health;
    private int love;

    public Pet() {
    }

    public Pet(String name, int health, int love) {
        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 abstract void adopt();
}

创建狗狗子类:

package com.zuoye1;

public class Dog extends Pet {
    private String strain;

    public Dog() {
    }

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


    public void adopt() {
        System.out.println("狗狗被领养。。。。。。。。。。");

    }

    public void playball() {
        System.out.println("带狗狗溜溜,并去草地玩皮球。。。。。。。。。。。");
    }
}

创建企鹅子类:

package com.zuoye1;

public class Penguin extends Pet {
    private String sex;

    public Penguin() {
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public void adopt() {
        System.out.println("企鹅被领养。。。。。。。。。。");
    }

    public void swimming() {
        System.out.println("带企鹅去游泳。。。。。。。。。。");
    }
}

创建小猫子类:

package com.zuoye1;

public class Cat extends Pet {
    private String colour;

    public Cat() {
    }

    public Cat(String name, int health, int love, String colour) {
        super(name, health, love);
        this.colour = colour;
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public void adopt() {
        System.out.println("小猫被领养。。。。。。。。。。");
    }

    public void climb() {
        System.out.println("带小猫去爬树。。。。。。。。。。。");
    }
}

创建主人类:

package com.zuoye1;

public class Master {
    public Pet adopt(Pet pet){
        if(pet instanceof Dog){
            Dog dog = (Dog)pet;
            dog.adopt();
        }else if(pet instanceof Cat){
            Cat cat = (Cat)pet;
            cat.adopt();
        }else if(pet instanceof Penguin){
            Penguin pen = (Penguin) pet;
            pen.adopt();
        }
        return pet;
    }
    public void playWithpet(Pet pet){
        if(pet instanceof Dog){
            Dog dog = (Dog)pet;
            dog.playball();
        }else if(pet instanceof Cat){
            Cat cat = (Cat)pet;
            cat.climb();
        }else if(pet instanceof Penguin){
            Penguin pen = (Penguin) pet;
            pen.swimming();
        }
    }
}

创建测试类:

package com.zuoye1;

public class TestMaster {
public static void main(String[] args) {
Master M = new Master();

Pet pet1 = new Cat();
Pet cat =M.adopt(pet1);
M.playWithpet(pet1);

Pet pet2 = new Dog();
Pet dog =M.adopt(pet2);
M.playWithpet(pet2);

Pet pet3 = new Penguin();
Pet pen =M.adopt(pet3);
M.playWithpet(pet3);
}
}
 

输出结果为:

小猫被领养。。。。。。。。。。
带小猫去爬树。。。。。。。。。。。
狗狗被领养。。。。。。。。。。
带狗狗溜溜,并去草地玩皮球。。。。。。。。。。。
企鹅被领养。。。。。。。。。。
带企鹅去游泳。。。。。。。。。。

 

posted @ 2019-01-22 10:37  BOZHU-liu  阅读(336)  评论(0编辑  收藏  举报