java多态领养_JavaOOP ( 三 ) 多态

题目:主人给三个宠物喂食操作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) {

this.name = name;

this.setHealth(health);

this.setLovely(lovely);

}

public String getName() {

return name;

}

public void setName(String name) {

this.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();

}

--------------------------------------------------

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

}

}

--------------------------------------------------

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

}

}

--------------------------------------------------

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

}

}

-----------------------------------------------

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

}

}

--------------------------------------

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

}

}

------------------------------------------

运行结果:

猫吃:鱼

健康值增加5,活跃度-5

我叫:张鑫鑫,我是一只黄色颜色的猫,,健康值为:85,活跃度为:55

够吃:狗粮

健康值增加3,活跃度-3

我叫:庞子谦,我是一只中华田园犬,健康值为:62,活跃度为:97

够吃:虾

健康值增加8,活跃度-8

我叫:邹自强,我是一只Q仔,健康值为:98,活跃度为:72

----------------------------------------------

该代码的问题:

疑问:喂养的方法出现方法的冗余,

如果再进行领养宠物,需要在此类中还需要添加feed方法(不方便修改,==添加新的功能要修改原来的代码)

方法名,参数列表,参数个数,方法的内容都是重复的、 唯一不一样的喂养动物的类型不一样

找规律:

所有喂养动物都是宠物,所以看下一个案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值