这篇文章主要为大家详细介绍了java多态实现电子宠物系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java多态实现电子宠物系统的具体代码,供大家参考,具体内容如下
效果实现:
代码实现:
1、主人类:
public class Master {
//领养
public Pet adopt(String name){
if ("狗狗".equals(name)){
return new Dog();
}else if (name.equals("企鹅")){
return new Penguin();
}
return null;
}
//喂食
public void feed(Pet pet, String food){
pet.eat(food);
}
//玩耍
public void play(Pet pet){
if(pet instanceof Dog){
Dog d= (Dog) pet;
d.catchDisk();
}else if(pet instanceof Penguin){
Penguin p= (Penguin) pet;
p.swimInSouth();
}
}
//捐献
public Pet donate(String name){
if("狗狗".equals(name)){
return new Dog();
}else if(name.equals("企鹅")){
return new Penguin();
}
return null;
}
}
2、狗狗类:
public class Dog extends Pet {
public void catchDisk(){
System.out.println("狗狗接飞盘!");
}
public Dog(int health) {
super(health);
}
public Dog() {
}
@Override
public void eat(String food) {
System.out.println("狗狗吃"+food+",吃饱后健康值+3");
setHealth(getHealth()+3);
}
}
3、企鹅类:
public class Penguin extends Pet{
public void swimInSouth(){
System.out.println("企鹅在南极游泳!");
}
public Penguin(int health) {
super(health);
}
public Penguin() {
}
@Override
public void eat(String food) {
System.out.println("企鹅宝宝吃"+food+",吃完后,健康值加5");
setHealth(getHealth()+5);
}
}
4、宠物类,Pet:
public class Pet {
private int health;
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
if(this.health>100){
this.health=100;
System.out.println("已经吃饱了,再吃就撑破了");
}
}
public Pet() {
}
public Pet(int health) {
this.health = health;
}
public void eat(String food){
System.out.println("宠物吃"+food);
}
}
5、测试类:
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Master master = new Master();
//领养
System.out.print("请输入您要领养的宠物:");
String dname = sc.next();
Pet p = master.adopt(dname);
//主人给狗狗喂食,玩耍
System.out.print("请输入您要喂得食物:");
String dfood = sc.next();
master.feed(p,dfood);
System.out.println(p.getHealth());
master.play(p);
//主人给企鹅喂食,玩耍
System.out.print("请输入您要玩的宠物:");
String pname = sc.next();
p = master.adopt(pname);
System.out.print("请输入您要喂的食物:");
String pfood = sc.next();
master.feed(p,pfood);
System.out.println(p.getHealth());
master.play(p);
//捐赠出去
System.out.print("请输入您要捐赠的宠物:");
String jname = sc.next();
Pet pet = master.donate(jname);
System.out.println();
System.out.println("您捐献的是:"+pet.getClass().getName());
}
}
补:这里测试类我是让用户在控制台输入,如果不想输入,可将测试类Test换成下面的代码:
public class TestPet {
public static void main(String[] args) {
Master m = new Master();
//领养
Pet p = m.adopt("狗狗");
//主人给狗狗喂食,玩耍
m.feed(p,"大骨头");
System.out.println(p.getHealth());
m.play(p);
//主人给企鹅喂食,玩耍
p = m.adopt("企鹅");
m.feed(p,"小鱼");
System.out.println(p.getHealth());
m.play(p);
//捐献出去
Pet pet = m.donate("狗狗");
System.out.println("您捐献的是:"+pet.getClass().getName());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:java多态实现电子宠物系统
猜你喜欢
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- Java实现顺序表的操作详解 2023-05-19
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- JSP页面间传值问题实例简析 2023-08-03
- JSP 制作验证码的实例详解 2023-07-30
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Java中的日期时间处理及格式化处理 2023-04-18
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- 深入了解Spring的事务传播机制 2023-06-02
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17