zoukankan      html  css  js  c++  java
  • 多态在宠物系统中的应用

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

    使用多态的两种形式:

    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("宠物的自白:
    我的名字叫 " + 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

    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

          

  • 相关阅读:
    20200813质因数分解 --已知正整数n是两个不同的质数的乘积,试求出较大的那个质数 (奥赛一本通 P71 8)
    20200807求梯形面积,要求输入浮点数,输出精度为2位
    c++语言printf()输出格式大全 scanf()输入格式大全
    20200803给出一 名学生的语文和数学成绩,判断他是否恰好有一门课不及格(<60分),如果是输出1;否则输出0(奥赛一本通 p32 10)
    20200803-判断一个数能否同时被3,5,7整除(奥赛一本通 p32 9)
    20200802--利用公式 e=1+1/1!+1/2!+...+1/n!,求e的值, 要求保留小数点后10位(奥赛一本通 p67 2)
    20200802 给定正整数n,求不大于n的正整数的阶乘的和(即求1!+2!+...+n!),输出阶乘的和 (奥赛一本通p67 1题)
    线程
    mysql逻辑架构
    《python网络数据采集》笔记2
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10643434.html
Copyright © 2011-2022 走看看