转载
有想转型软设,架构的小伙伴吗?运维、测试、产品、程序员如何发展?关注我,我分享我如何2个月考过软考架构证书,并突破年薪50万的。
https://d.51cto.com/bLN8S1
题目:主人给三个宠物喂食操作Pet.java 宠物类代码如下:
package com.part1; /** * 1.宠物抽象类 * 该类用到的内容: * 封装:隐藏类的实现细节,限定非法操作 * 构造方法:在创建对象时,完成属性的初始化操作 * 抽象方法:没有必要添加实现细节 */ public abstract class Pet { private String name; //姓名 private int health; //健康值 private int lovely; //活跃度 public Pet() { } public Pet(String name, int health, int lovely) { = name; this.setHealth(health); this.setLovely(lovely); } public String getName() { return name; } public void setName(String name) { = name; } public int getHealth() { return health; } public void setHealth(int health) { if(health<0 ||="" health="">100){ System.out.println('非法赋值'); this.health=60; }else{ this.health = health; } } public int getLovely() { return lovely; } public void setLovely(int lovely) { if(lovely<0 ||="" lovely="">100){ System.out.println('非法赋值'); this.lovely=60; }else{ this.lovely = lovely; } } /** * 吃的 * @param food */ public abstract void eat(String food); /** * 叫的方法 */ public abstract void say(); } --------------------------------------------------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.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.
2.子类Cat.java宠物猫类
package com.part1; /** * 猫类 用到的内容:继承 如果一个普通类继承了抽象类:必须要重写抽象类的所有的抽象方法 * @author Administrator * */ public class Cat extends Pet { private String color;//颜色 public Cat() { } public Cat(String name, int health, int lovely, String color) { super(name, health, lovely); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void eat(String food) { System.out.println('猫吃:'+food); System.out.println('健康值增加5,活跃度-5'); this.setHealth(this.getHealth()+5); this.setLovely(this.getLovely()-5); } @Override public void say() { System.out.println('我叫:'+super.getName() +',我是一只'+this.color+'颜色的猫,' +',健康值为:'+super.getHealth() +',活跃度为:'+super.getLovely()); } }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.29.30.31.32.33.34.35.36.37.
--------------------------------------------------
3.宠物狗Dog.java类
package com.part1; public class Dog extends Pet{ private String strain; //品种 public Dog() { super(); } public Dog(String name, int health, int lovely, String strain) { super(name, health, lovely); this.strain = strain; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } @Override public void eat(String food) { System.out.println('够吃:'+food); System.out.println('健康值增加3,活跃度-3'); this.setHealth(this.getHealth()+3); this.setLovely(this.getLovely()-3); } @Override public void say() { System.out.println('我叫:'+super.getName() +',我是一只'+this.strain +',健康值为:'+super.getHealth() +',活跃度为:'+super.getLovely()); } }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.29.30.31.
--------------------------------------------------
4.宠物企鹅Penguin.java类
package com.part1; public class Penguin extends Pet{ private String sex; //性别 public Penguin() { super(); } public Penguin(String name, int health, int lovely, String sex) { super(name, health, lovely); this.sex = sex; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public void eat(String food) { System.out.println('够吃:'+food); System.out.println('健康值增加8,活跃度-8'); this.setHealth(this.getHealth()+8); this.setLovely(this.getLovely()-8); } @Override public void say() { System.out.println('我叫:'+super.getName() +',我是一只'+this.sex +',健康值为:'+super.getHealth() +',活跃度为:'+super.getLovely()); } }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.29.30.31.
-----------------------------------------------
5.主人类Master.java
package com.part1;
/**
* 疑问:喂养的方法出现方法的冗余,
* 如果再进行领养宠物,需要在此类中还需要添加feed方法(不方便修改,==添加新的功能要修改原来的代码)
* 方法名,参数列表,参数个数,方法的内容都是重复的、
* 唯一不一样的喂养动物的类型不一样
* 找规律:
* 所有喂养动物都是宠物,所以看以下个包
* @author Administrator * */ public class Master { /** * 主人给猫喂食操作 */ public void feed(Cat pet,String food){ pet.eat(food); } /** * 主人给狗喂食操作 */ public void feed(Dog pet,String food){ pet.eat(food); } /** * 主人给企鹅喂食操作 */ public void feed(Penguin pet,String food){ pet.eat(food); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
--------------------------------------
6.测试类Test.java
package com.part1; public class Test { public static void main(String[] args) { Master master=new Master(); Cat cat=new Cat('张鑫鑫', 80, 60, '黄色'); master.feed(cat, '鱼'); cat.say(); Dog dog=new Dog('庞子谦', 59, 100, '中华田园犬'); master.feed(dog, '狗粮'); dog.say(); Penguin pen=new Penguin('邹自强', 90, 80, 'Q仔'); master.feed(pen, '虾'); pen.say(); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.
------------------------------------------
运行结果:
猫吃:鱼
健康值增加5,活跃度-5
我叫:张鑫鑫,我是一只黄色颜色的猫,,健康值为:85,活跃度为:55
够吃:狗粮
健康值增加3,活跃度-3
我叫:庞子谦,我是一只中华田园犬,健康值为:62,活跃度为:97
够吃:虾
健康值增加8,活跃度-8
我叫:邹自强,我是一只Q仔,健康值为:98,活跃度为:72
----------------------------------------------
该代码的问题:
疑问:喂养的方法出现方法的冗余,
如果再进行领养宠物,需要在此类中还需要添加feed方法(不方便修改,==添加新的功能要修改原来的代码)
方法名,参数列表,参数个数,方法的内容都是重复的、 唯一不一样的喂养动物的类型不一样
找规律:
所有喂养动物都是宠物,所以看下一个案例
有想转型软设,架构的小伙伴吗?运维、测试、产品、程序员如何发展?关注我,我分享我如何2个月考过软考架构证书,并突破年薪50万的。
https://d.51cto.com/bLN8S1
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
相关文章