Java面向对象第四章方法重写与多态练习题

练习1:使用重写优化电子宠物系统

需求说明 使用方法重写优化电子宠物系统,实现如下效果

 

package com.hz.ch02;
/**
 * 猫类
 * @author 26255
 *
 */
public class Cat extends Father {
	private  String sex;

	
	public Cat(String name, int health, int love, String sex) {
		super(name, health, love);
		this.sex = sex;
	}
	public void print() {
		super.print();
		System.out.println(",品种"+this.getSex());
	}
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
}
package com.hz.ch02;
/**
 * 狗类
 * @author 26255
 *
 */
public class Dog extends Father{
	private String  type;

	public Dog(String name, int health, int love, String type) {
		super(name, health, love);
		this.type = type;
	}
	public void print() {
		super.print();
		System.out.println(",品种"+this.getType());
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	
}

 

package com.hz.ch02;
/**
 * 父类
 * @author 26255
 *
 */
public class Father {
	private String name;
	private int health;
	private int love;
	
	
	
	
	
	public Father(String name, int health, int love) {
		super();
		this.name = name;
		this.health = health;
		this.love = love;
	}
	public void print() {
		System.out.println("我的名字是"+this.getName()+",健康值"+this.getHealth()+",亲密度"+this.getLove());
	
	}
	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;
	}
	
}

 

package com.hz.ch02;

public class Lianx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Dog d = new Dog("白菜",100,100,"哈士奇");
		d.print();
		Cat s = new Cat("为2",60,70,"雌性");
		s.print();
	}

}

 运行:

我的名字是白菜,健康值100,亲密度100
,品种哈士奇
我的名字是为2,健康值60,亲密度70
,品种雌性

 

 练习2:重写equals()

需求说明 重写比较规则,判断两名学员(Student)是否为同一对象 Student相关属性 Id(学号)、name(姓名)、age(年龄) 如果两名学员的学号以及姓名相同,则为同一对象

instanceof用于判断一个引用类型所引用的对象是否是一个类的实例

package com.hz.ch01;
/**
 * 学生类
 */
public class Father {
	
	private String name;
	private String id;
	private int age;
	
	public boolean equals(Object obj) {
		if(obj instanceof Father) {
		Father m1 = (Father) obj;
		return this.name==m1.name&&this.id==m1.id;
		}
		
		return false;
	}

	public Father(String name, String id, int age) {
	
		this.name = name;
		this.id = id;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
		
	
	
	
}

 

package com.hz.ch01;

public class Lianx1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Father m = new Father("张三","qq",25);
		Father m1 = new Father("张三","qq",20);
		System.out.println(m.equals(m1));
	}

}

 运行:

true

练习3:使用多态实现为宠物喂食

需求说明 宠物饿了,主人需要为宠物喂食,使用多态实现该过程 不同宠物吃的东西不一样 不同宠物吃完东西后恢复健康值不一样 健康值达到100时,不需要继续喂食

 

package com.hz.ch03;
/**
 * 父类
 * @author 26255
 *
 */
public class Pet {
	private int health;
	
	
	public Pet(int health) {
	
		this.health = health;
	}

	public int getHealth() {
		return health;
	}
	//判断健康值
	public void setHealth(int health) {
		this.health = health;
		if(this.health>100) {
			this.health=100;
			System.out.println("健康值已到100,不需要继续喂食");
		}
		
	}
	public void eat(String un) {
		
	}
	
	
	
}
package com.hz.ch03;
/**
 * 狗狗类
 * @author 26255
 *
 */
public class Dog extends Pet{

	public Dog(int health) {
		super(health);
	
	}

	public void eat(String un) {
		System.out.println("狗狗的初始健康值"+getHealth()+",狗狗想吃"+un+"狗狗吃到了"+un+",健康值加三3");
		setHealth(getHealth()+3);
	}
	
}
package com.hz.ch03;
/**
 * 企鹅类
 * @author 26255
 *
 */
public class Penuin extends Pet{
	public Penuin(int health) {
		super(health);
	
	}

	public void eat(String un) {
		System.out.println("企鹅的初始健康值"+getHealth()+",企鹅想吃"+un+"企鹅吃到了"+un+",健康值加三5");
		setHealth(getHealth()+5);
	}
}

 

package com.hz.ch03;
/**
 * 主人类
 * @author 26255
 *
 */
public class Person {
	public void un(Pet pet,String un) {
		pet.eat(un);
		String s = un;
	}

	
}

 

package com.hz.ch03;

public class Lianx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person person = new Person();
		Pet dog  = new Dog(70);
		person.un(dog,"骨头");
		System.out.println("狗狗吃完后的健康值是:"+dog.getHealth());
		
		Pet penuin = new Penuin(80);
		person.un(penuin, "鱼");
		System.out.println("企鹅吃完后的健康值是:"+penuin.getHealth());
	}

}

 运行:

狗狗的初始健康值70,狗狗想吃骨头狗狗吃到了骨头,健康值加三3
狗狗吃完后的健康值是:73
企鹅的初始健康值80,企鹅想吃鱼企鹅吃到了鱼,健康值加三5
企鹅吃完后的健康值是:85

练习4:使用多态实现主人领养宠物并与宠物玩耍功能

需求说明 狗具有特有的接飞盘方法,企鹅具有特有的南极游泳方法。请编写测试类分别调用各种具体宠物的特有方法 使用向下转型 使用instanceof判断宠物类型

package com.hz.ch04;
/**
 * 父类
 * @author 26255
 *
 */
public class Pet {
	private String name;
	private int health=80;//健康值
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLove() {
		return love;
	}

	public void setLove(int love) {
		this.love = love;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	private int love=50;//亲密度
	
	public Pet(String name, int health, int love) {
		super();
		this.name = name;
		this.health = health;
		this.love = love;
	}
	
	public int getHealth() {
		return health;
	}
	
	
	
	
}

 

package com.hz.ch04;
/**
 * 主人类
 * @author 26255
 *
 */
public class Person {
	private String name;
	private int age;
	
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public void play(Pet pet) {
		if(pet instanceof Dog) {
			Dog dog = (Dog)pet;
			dog.fly();
		}else if(pet instanceof Penuin) {
			Penuin p = (Penuin)pet;
			p.sw();
		}
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
	
	
}
package com.hz.ch04;
/**
 * 狗狗类
 * @author 26255
 *
 */
public class Dog extends Pet{
	private String strain;//品种

	
	public Dog(String name, int health, int love, String strain) {
		super(name, health, love);
		this.strain = strain;
	}
	public void fly() {
		System.out.println("小狗正在接飞盘!");
		System.out.println("健康值:"+super.getHealth()+10);
		System.out.println("亲密度:"+super.getLove()+5);
	}
	public String getStrain() {
		return strain;
	}

	public void setStrain(String strain) {
		this.strain = strain;
	}
	
	
	
	
}
package com.hz.ch04;
/**
 * 企鹅类
 * @author 26255
 *
 */
public class Penuin extends Pet{
	private String sex;

	public Penuin(String name, int health, int love, String sex) {
		super(name, health, love);
		this.sex = sex;
	}
	
	public void sw() {
		System.out.println("企鹅正在滑冰!");
		System.out.println("健康值:"+super.getHealth()+10);
		System.out.println("亲密度:"+super.getLove()+5);
	}
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	

}
package com.hz.ch04;

public class Lianx {

	public static void main(String[] args) {
		Person m = new Person(null, 0);
		m.play(new Dog("zhang ", 0, 0, "hiw"));
		m.play(new Penuin("hduh ", 0, 0, "fwh"));
	}
	
}

 运行:

小狗正在接飞盘!
健康值:010
亲密度:05
企鹅正在滑冰!
健康值:010
亲密度:05

 

练习5:打印商品价格

需求说明 自定义类和方法,使用父类作为返回值实现打印不同类型商品价格功能 父类:Goods(商品类) 子类:TVs(电视类)、Foods(食品类)

package com.hz.ch05;
/**
 * 厂家
 * @author 26255
 *
 */
public class Plant {
	
	public Goods Goods(String type) {
		Goods goods = null;
		if(type.equals("tv")) {
			goods = new TVs();
		}else if(type.equals("食品")) {
			goods = new Foods();
			
		}
		return goods;	
		
	}
}
package com.hz.ch05;
/**
 * 食品类
 * @author 26255
 *
 */
public class Foods extends Goods{
	public void print() {
		System.out.println("食品价格:100");
	}
}
package com.hz.ch05;
/**
 * 父类
 * @author 26255
 *
 */
public abstract class Goods {
	private int price;//价格

	

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}
	public abstract void print();
}

 

package com.hz.ch05;
/**
 * 电视类
 * @author 26255
 *
 */
public class TVs extends Goods{
	public void print() {
		System.out.println("电视价格:4999");
	}
}

 

package com.hz.ch05;

public class Lianxi {
	public static void main(String[] args) {
		Plant p1 = new Plant();
		Goods goods = p1.Goods("tv");
		goods.print();
		goods = p1.Goods("食品");
		goods.print();
	}
}

运行:

电视价格:4999
食品价格:100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒洋洋大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值