java类的多态,向下转型

类似于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;
 } 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Exception.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值