Pet类
package com.neusoft;
public abstract class Pet {
public String name;
public int health;
public int love;
public Pet() {
super();
}
public Pet(String name, int health, int love) {
super();
this.name = name;
this.health = health;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
if(health >= 100){
health = 100;
}
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
if(love >= 100){
love = 100;
}
this.love = love;
}
public abstract void print();
public abstract void eat();
public abstract void cure();
}
Dog类
package com.neusoft;
import java.util.Scanner;
public class Dog extends Pet {
Scanner sc = new Scanner(System.in);
private String strain;// 品种
public Dog(String name, int health, int love, String strain) {
super();
}
public Dog() {
super();
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("狗狗吃骨头");
super.health += 3;
print();
}
/**
* 狗特有的接飞盘的方法
*/
public void catchFlyDisc() {
System.out.println("狗在接飞盘");
// 主人和狗狗玩接飞盘游戏,狗狗健康值减少5,与主人亲密度增加10
super.health -= 5;
super.love += 10;
print();
}
/**
* 给狗狗看病的方法
*/
public void cure() {
if (super.health <= 50) {
System.out.println("带狗狗看病去了");
}
while (super.health <= 50) {
super.health += 20;
}
print();
}
public void chooseStrain() {
System.out.println("请选择狗的类型 1.金毛 2.哈士奇");
do {
int choice = sc.nextInt();
if (choice == 1) {
setStrain("金毛");
} else if (choice == 2) {
setStrain("哈士奇");
}
} while (getStrain() == null);
}
@Override
public void print() {
System.out.println("我是一只品种为" + this.strain + "的狗,我的名字是"
+ super.getName() + ",我的健康值是" + super.getHealth() + ",我的亲密度是"
+ super.getLove());
}
}
Penguin类
package com.neusoft;
import java.util.Scanner;
public class Penguin extends Pet {
Scanner sc = new Scanner (System.in);
private String sex;
public Penguin() {
super();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Penguin(String name, int health, int love, String sex) {
super();
}
@Override
public void eat() {
System.out.println("企鹅吃鱼");
super.health += 3;
print();
}
/**
* 企鹅特有的游泳方法
*/
public void swimming() {
System.out.println("企鹅在游泳");
// 主人和企鹅玩游泳游戏,企鹅健康值减少5,与主人亲密度增加10
super.health -= 5;
super.love += 10;
print();
}
/**
* 带企鹅看病的方法
*/
public void cure() {
if (super.health <= 50) {
System.out.println("带企鹅看病去了");
}
while (super.health < 50) {
super.health += 20;
}
print();
}
public void chooseSex(){
System.out.println("请选择你想要领养的企鹅的性别 1.Q仔 2.Q妹");
System.out.println("请输入你的选择");
int i = new Scanner(System.in).nextInt();
String sex = null;
if (i == 1) {
sex = "Q仔";
setSex(sex);
set();
} else if (i == 2) {
sex = "Q妹";
setSex(sex);
set();
}
}
public void set(){
System.out.println("请输入昵称");
super.name= sc.next();
System.out.println("请输入健康度");
super.health = sc.nextInt();
System.out.println("请输入亲密度");
super.love = sc.nextInt();
}
@Override
public void print() {
System.out.println("我是一只性别为" + this.sex + "的企鹅,我的名字是" + super.getName()
+ ",我的健康值是" + super.getHealth() + ",我的亲密度是" + super.getLove());
}
}
Master类
package com.neusoft;
import java.util.Scanner;
public class Master {
Scanner sc = new Scanner(System.in);
public void feed(Pet p){
p.eat();
}
/**
* 主人与宠物玩耍的功能
* 判断传入参数的类型
* 进行向下转型
* @param pet
* 父类作为形式参数
*/
public void Play(Pet pet){
if(pet instanceof Dog){
Dog dog = (Dog)pet;
dog.catchFlyDisc();
}else if(pet instanceof Penguin){
Penguin p = (Penguin)pet;
p.swimming();
}
}
public Pet adoptPet(int choice) {
Pet pet = null;
System.out.print("请输入要领养的宠物名字:");
String name = sc.next();
switch (choice) {
case 1:
pet = new Dog();
Dog dog = (Dog) pet;
dog.chooseStrain();
System.out.println("输入狗狗健康值:");
dog.setHealth(sc.nextInt());
System.out.println("请输入狗狗的亲密度");
dog.setLove(sc.nextInt());
dog.setName(name);
pet.print();
break;
case 2:
pet = new Penguin();
Penguin pen = (Penguin) pet;
pen.chooseSex();
pen.setName(name);
pen.print();
break;
}
return pet;
}
public void cure(Pet pet) {
System.out.println("给宠物看病");
pet.cure();
}
}
PetTest类
package com.neusoft;
import java.util.Scanner;
public class PetTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Master m = new Master();
Pet p = null;
System.out.println("欢迎您来到宠物店");
System.out.println("请选择要领养的宠物类型(1.狗类 2.企鹅类)");
int choice = sc.nextInt();
p = m.adoptPet(choice);
System.out.println("游戏类型:(1.喂食 2.玩耍 3.看病)");
int type = sc.nextInt();
do {
switch (type) {
case 1:
m.feed(p);
System.out.println("选择游戏类型:");
type = sc.nextInt();
break;
case 2:
m.Play(p);
System.out.println("选择游戏类型:");
type = sc.nextInt();
break;
case 3:
m.cure(p);
System.out.println("选择游戏类型:");
type = sc.nextInt();
break;
default:
System.exit(0);
break;
}
} while (type != 1 || type != 2 || type != 3);
}
}