父類作為方法形參實現多態(父類作為返回值)待加


寵物類

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

還得繼續完善


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM