Lev1

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

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

宠物类

复制代码
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()); 
           
        }
}
复制代码

还得继续完善

posted on   Lev1  阅读(1181)  评论(0编辑  收藏  举报

编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
点击右上角即可分享
微信分享提示