类似于int i=(double)5.5;数据类型的强转。就是把父类强转子类。打个不恰当的比喻就是,你爸爸叫儿子"爸爸"
main 函数,程序入口
public class Test {
public static void main(String[] args) {
Pet pet=new Dog("","");//懒得再在dog类里面加个默认的构造函数了,所以用的这个
//pet.eat();
Master m=new Master();
//m.play(pet);
System.out.println("请输入领养的宠物1.狗 2.企鹅");
Scanner input=new Scanner(System.in);
int choose =input.nextInt();
pet=m.getPet(choose);
if (pet!=null) {
System.out.println("领养成功");
m.feed(pet);
m.play(pet);
}
}
}
主人类,独立于宠物,拥有人的行为,比如陪宠物玩,领养宠物,喂宠物吃饭
public class Master {
public void feed(Pet pet) {//主人喂宠物
pet.eat();
}
public void play(Pet pet) {//主人和宠物玩。。。 用父类作为带参
if (pet instanceof Dog) {
Dog d=(Dog)pet;
d.play();
}else if (pet instanceof Penguin) {
Penguin p=(Penguin)pet;
p.play();
}
}
public Pet getPet(int choose) {//领养宠物 //用父类作为方法
Pet pet=null;
if (choose==1) {
pet=new Dog("球球","茶杯");
}else if (choose==2) {
pet =new Penguin("豆豆","Q妹");
}
return pet;
}
}
父类,宠物类,一个用于存储动物的公共参数,方法的地方。宠物的公共行为
public abstract class Pet {
private String name;
private int health =50;
private int love = 0;
public Pet(String name) {
this.name = name;
}
public abstract void eat();//抽象方法,用于子类的继承和重写
public abstract void show();//抽象方法,用于子类的继承和重写
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;
}
}
子类 dog
public class Dog extends Pet{
private String pinzhong;
public Dog(String name,String pinzhong) {
super(name);
this.pinzhong = pinzhong;
}
public void play(){
if (super.getHealth()>=10){
System.out.println("狗狗"+super.getName()+"在接飞盘");
int health=super.getHealth()-10;
int love= super.getLove()+5;
System.out.println(health+"..."+love);
}else {
System.out.println("健康值不足");
}
}
public void eat() {
System.out.println("狗狗在吃狗粮。。。。");
if(super.getHealth()+3<=100) {
super.setHealth(super.getHealth()+3);
}else {
super.setHealth(100);
}
}
@Override
public void show() {
System.out.println("我是:"+super.getName()+",健康值是:"+super.getHealth()+",亲密度是:"+super.getLove()+",品种是:"+pinzhong);
}
public String getPinzhong() {
return pinzhong;
}
public void setPinzhong(String pinzhong) {
this.pinzhong = pinzhong;
}
}
子类penguin 企鹅
public class Penguin extends Pet{
private String sex;
public Penguin(String name,String sex) {
super(name);
this.sex = sex;
}
@Override
public void eat() {
System.out.println("企鹅在吃鱼。。。。");
if(super.getHealth()+5<=100) {
super.setHealth(super.getHealth()+5);
}else {
super.setHealth(100);
}
}
public void play(){
if (super.getHealth()>=10){
System.out.println("企鹅"+super.getName()+"在游泳");
int health=super.getHealth()-10;
int love= super.getLove()+5;
System.out.println(health+"..."+love);
}else {
System.out.println("健康值不足");
}
}
@Override
public void show() {
System.out.println("我是:"+super.getName()+",健康值是:"+super.getHealth()+",亲密度是:"+super.getLove()+",性别是:"+sex);
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}