标签: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