S2_使用java面向对象编程第3章上机

练习1:使用多态实现给宠物喂食功能

package cn.jbit.epet.poly2;
/**
 * 猫类,宠物的子类。
 */
public class Cat extends Pet {
   
	private String color;//颜色
	public Cat(String name, String color) {
   
		super(name);
		this.color = color;
	}
	
	public void setColor(String color) {
   
		this.color = color;
	}

	public String getColor() {
   
		return color;
	}
	/**
	 *  实现吃饭方法 
	 */
	public void eat() {
   
		if(getHealth()>=100){
   
			System.out.println("狗狗"+this.getName() +"吃饱了,不需要喂食了!");
		}else{
   
			this.setHealth(this.getHealth()+4);
			System.out.println("猫咪"+this.getName() + "吃饱啦!体力增加4。");
		}
	}
}

package cn.jbit.epet.poly2;

/**
 * 狗狗类,宠物的子类。
 */
public class Dog extends Pet {
   
	private String strain;// 品种
	/**
	 * 有参构造方法。
	 * @param name   昵称
	 * @param strain   品种
	 */
	public Dog(String name, String strain) {
   
		super(name); 
		this.strain = strain;
	}
	public String getStrain() {
   
		return strain;
	}
	/**
	 * 重写父类的print方法。
	 */
	public void print(){
   
		super.print(); //调用父类的print方法
		System.out.println("我是一只 " + this.strain + "。");
	}
	
	/**
	 * 实现吃食方法。 
	 */
	public void eat() {
   
		if(getHealth()>=100){
   
			System.out.println("狗狗"+this.getName() +"吃饱了,不需要喂食了!");
		}else{
   
			this.setHealth(this.getHealth()+3);
			System.out.println("狗狗"+this.getName() + "吃饱啦!健康值增加3。");
		}
	}
}

package cn.jbit.epet.poly2;

/**
 * 主人类。
 */
public class Master {
   
	private String name = "";// 主人名字
	private int money = 0; // 元宝数
	/**
	 * 有参构造方法。
	 * @param name 主人名字
	 * @param money 元宝数
	 */
	public Master(String name, int money) {
   
		this.name = name;
		this.money = money;
	}
	
	public void setName(String name) {
   
		this.name = name;
	}

	public void setMoney(int money) {
   
		this.money = money;
	}

	public int getMoney() {
   
		return money;
	}
	public String getName() {
   
		return name;
	}
	/**
	 * 主人给宠物喂食。
	 */
	public void feed(Pet pet) {
   
		pet.eat();
	}
}

package cn.jbit.epet.poly2;

/**
 * 企鹅类,宠物的子类。
 */
public class Penguin extends Pet {
   
	private String sex;// 性别
	/**
	 * 有参构造方法。
	 * @param name 昵称
	 * @param sex 性别
	 */
	public Penguin(String name, String sex) {
   
		super(name);
		this.sex = sex;
	}
	public String getSex() {
   
		return sex;
	}
	/**
	 * 重写父类的print方法。
	 */
	public void print() {
   
		super.print();
		System.out.println("性别是 " + this.sex + "。");
	}
	
	/**
	 * 实现吃食方法。 
	 */
	public void eat() {
   
		if(getHealth()>=100){
   
			System.out.println("企鹅"+this.getName() +"吃饱了,不需要喂食了!");
		}else{
   
			this.setHealth(this.getHealth()+5);
			System.out.println("企鹅"+this.getName() + "吃饱啦!健康值增加3。");
		}
	}
}

package cn.jbit.epet.poly2;

/**
* 宠物类,狗狗和企鹅的父类。
*/
public abstract class Pet {
   
	private String name = "无名氏";// 昵称
	private int health = 100;// 健康值
	private int love = 0;// 亲密度
	
	/**
	 * 抽象方法eat(),负责宠物吃饭功能。
	 */
	public abstract void eat();
	
	/**
	 * 有参构造方法。
	 * @param name  昵称
	 */
	public Pet(){
   
		
	}
	
	public Pet(String name) {
   
		this.name = name;
	}
	
	public void setName(String name) {
   
		this
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值