父类作为方法形参实现多态(父类作为返回值)待加

宠物类

package com.pangui;
//父类
public  class CongWu {
    private String name = "无名氏";
    private int health ;
    private int love;
//    public CongWu() {
//        //无参构造方法
//    }
    public CongWu(String name,int health,int love) {
        //带参构造方法
        this.name = name;
        if(health < 0||health > 100) {
            //System.out.println("健康值应在0-100之间,默认值为60");
            this.health = 60;
        }else {
            this.health = health;
        }
        if(love>100||love<0) {
            //System.out.println("请密度应在0-100之间,默认值为60");
            this.love = 60;
        }else {
            this.love = love;
        }
//        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 show() {
        System.out.println("宠物的自白:\n我的名字叫"+this.name+",我与主人的亲密度是"+this.love+",健康值"+ this.health+"。");
    }
    public void Play(){
        
    }
    public void bath() {
        System.out.println("主人在为"+this.getName()+"洗澡");
    }
    public void eat() {}
    

狗类

package com.pangui;
//狗类
public  class Dog extends CongWu{
        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 show() {                         //重写
            super.show();     
            System.out.println("我是一只"+this.getStrain()+"狗");// ????????????
        }
        public void xizao() {
            System.out.println(this.getName()+"在洗澡");
        }
        public void Play(){
            System.out.println(this.getName()+"在玩蛇");
        }
        public void eat() {
            if(this.getHealth() == 100) {
                System.out.println("狗狗"+this.getName()+"吃饱了,不需要喂食");
            }else {
                System.out.println("狗狗"+this.getName()+"吃根骨头");
                this.setHealth(this.getHealth()+3);
            }
        }
        public void catchFly() {
            System.out.println("狗狗叼飞碟");
        }
}

企鹅类

package com.pangui;
//企鹅类
public  class QiE extends CongWu {
    private String sex;
//    public QiE() {
//    }
    public QiE(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 show() {
        super.show();
        System.out.println("我的性别是"+this.getSex());
    }
    public void Play(){
        System.out.println(this.getName()+"在玩水");
    }
    public void eat() {
        if(this.getHealth() == 100) {
            System.out.println("企鹅"+this.getName()+"吃饱了,不需要喂食");
        }else {
            System.out.println("企鹅"+this.getName()+"吃小鱼");
            this.setHealth(this.getHealth()+5);
        }
    }
}

主人类

package com.pangui;
//主人类
public class Master {
       public void game(CongWu congwu) {     //方法形参
          congwu.Play(); 
        }
       public void feed(CongWu congwu) {
            congwu.eat();
       }
//       public Dog shower() {
//           Dog dog = null;
//                      
//        return dog;
//       }
       
}
 

测试类

package com.pangui;
//测试类
public class Test {
        public static void main(String[] args) {
             Master master = new Master();
            CongWu dog = new Dog("多多",100,87,"哈士奇");//向上转型
            master.feed(dog);
            master.game(dog);    
            Dog d = (Dog)dog;//向下转型
            d.catchFly();
            CongWu qie = new QiE("豆豆",87,56,"Q仔");
            master.feed(qie);
            master.game(qie);
            
          //System.out.println(master.shower()); 
           
        }
}

还得继续完善

转载于:https://www.cnblogs.com/lev1/p/11217563.html

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 首先,我们可以创建一个宠物类 Pet 作为父类,包含宠物的基本属性和行为。然后,我们可以创建三个子类 Dog、Cat 和 Fish,分别继承宠物类 Pet,并且重写一些方法实现不同的行为。 代码示例: ```python class Pet: def __init__(self, name, age): self.name = name self.age = age self.health = 100 def eat(self): self.health += 10 def play(self): self.health -= 10 class Dog(Pet): def __init__(self, name, age, breed): super().__init__(name, age) self.breed = breed def bark(self): print("汪汪!") class Cat(Pet): def __init__(self, name, age, color): super().__init__(name, age) self.color = color def meow(self): print("喵喵!") class Fish(Pet): def __init__(self, name, age, species): super().__init__(name, age) self.species = species def swim(self): print("游泳!") ``` 接下来,我们可以创建一个主人类 Owner,包含领养宠物和喂养宠物的方法。这些方法将以父类 Pet 作为形参返回值实现多态。 代码示例: ```python class Owner: def __init__(self, name): self.name = name self.pets = [] def adopt_pet(self, pet): self.pets.append(pet) def feed_pet(self, pet): pet.eat() ``` 最后,我们可以创建一个测试函数,创建一个主人对象和三个宠物对象,然后领养宠物并喂养宠物。 代码示例: ```python def test(): # 创建主人对象 owner = Owner("张三") # 创建宠物对象 dog = Dog("旺财", 2, "拉布拉多") cat = Cat("小白", 1, "白色") fish = Fish("小鱼", 1, "金鱼") # 领养宠物 owner.adopt_pet(dog) owner.adopt_pet(cat) owner.adopt_pet(fish) # 喂养宠物 for pet in owner.pets: owner.feed_pet(pet) ``` 这样,我们就成功地使用父类作为方法形参返回值实现多态,并且使用多态实现了主人领养三只宠物并喂养宠物的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值