码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-20 14:29:30      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:lov   void   lse   static   mic   image   企鹅   show   new   

宠物类

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

技术图片

还得继续完善

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

标签:lov   void   lse   static   mic   image   企鹅   show   new   

原文地址:https://www.cnblogs.com/lev1/p/11217563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!