多态的理解和使用

56.多态

作用:优化代码,解决代码臃肿

现实中多态:同一种事物,由于条件不同,产生的结果也不同

编程多态:同一个引用类型,使用不同的实例而执行不同的操作

继承和方法重写是实现多态的基础

应用:父类引用指向子类对象

//主人类

public class Master {
    //为宠物看病:如果宠物健康值小于50,就要去宠物医院看病
    public void cure(Pet pet){
        if(pet.getHealth()<50){
            pet.toHospital();
        }
    }

    //给宠物喂食
    public void feed(Pet pet){
        pet.eat();
    }
}

//父类,提取共性代码Pet类

public class Pet {
    //1.隐藏属性(添加private)
    //昵称,默认值是“无名氏”
    private String name="无名氏";
    //健康值,默认值是100,健康值在0-100之间,小于60为不健康
    private int health=100;
    //亲密度
    private int love=0;

    public Pet(){
        System.out.println("父类无参构造方法");
    }
    public Pet(String name){
        this.name=name;
    }
    public Pet(String name,int health,int love){
        //this(name);  //this可调用本类的构造方法,且必须在第一行
        this.name=name;
        this.health=health;
        this.love=love;
        System.out.println("父类的带参构造方法");
    }

    //2.添加属性的setter/getter方法(方法公开),并加入属性控制语句
    //setter:1.属性赋值。2.属性的操作(正确判断等)
    public void setHealth(int health){
        if(health<0||health>100){
            System.out.println("请输入0-100的值!");
            this.health=60;
            return;
        }
        this.health=health;
    }
    //getter:属性取值
    public int getHealth(){
        return this.health;
    }

    public String getName() {
        return this.name;
    }

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

    public int getLove() {
        return this.love;
    }

    public void setLove(int love) {
        if(love<0||love>100){
            System.out.println("请输入0-100的值!");
            this.love=60;
            return;
        }
        this.love=love;
    }

     /**
      * 输出宠物的信息
     * */
    public void print(){
        System.out.println("宠物的自白:\n我的名字叫"+this.name+",健康值为"+this.health+
                ",和主人的亲密度为"+this.love+"。");
//        if(this.health<0||this.health>100){
//            System.out.println("请输入0-100的值!");
//            this.health=60;
//        }else{
//            this.health=health;
//        }
    }

    //宠物生病后看病
    public void toHospital(){

    }

}

/*
* 宠物狗狗类Pet类
* */

public class Dog extends Pet{

    //品种
    private String strain="聪明的拉布拉多犬";


    public Dog(){
        System.out.println("子类狗狗的无参构造方法");
    }
    public Dog(String name,int health,int love){
        super(name,health,love);
        System.out.println("子类狗狗带三个参数的构造方法");
    }
    public Dog(String name,int health,int love,String strain){
        /*//通过super调用父类的构造方法,必须是第一行
        //super();注意参数顺序必须和父类一致
        super(name,health,love);*/
        this(name,health,love);
        this.strain=strain;
        System.out.println("子类狗狗的带四个参数的构造方法");
    }


    public String getStrain() {
        return this.strain;
    }

    public void setStrain(String strain) {
        this.strain = strain;
    }

    public void print(){
        //调用父类的非private方法print()
        super.print();
        System.out.println(",我是一只:"+this.strain);
    }

    //宠物生病后看病
    public void toHospital(){
        System.out.println("打针、吃药");
        this.setHealth(60);
    }

}

/*
* 宠物企鹅类Pet类
* */

public class Penguin extends Pet {

    //性别
    private String sex="Q仔";

    public Penguin(){
        System.out.println("子类企鹅的无参构造方法");
    }
    public Penguin(String name,int health,int love,String sex){
        super(name,health,love);
        this.sex=sex;
        System.out.println("子类企鹅带三个参数的构造方法");
    }

    public String getSex() {
        return this.sex;
    }

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

    public void print(){
        //调用父类的print()
        super.print();
        System.out.println(",我的性别是:"+this.sex);
    }
    //宠物生病后看病
    public void toHospital(){
        System.out.println("吃药、疗养");
        this.setHealth(60);
    }
}

//宠物测试类TestPet类

import java.util.Scanner;
public class TestPet {
    public static void main(String[] args) {
        Master master=new Master();
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎来到宠物店!");
        System.out.println("请输入您要领养宠物的名字:");
        String name = input.next();
        System.out.println("请输入您要领养的宠物类型:1.狗狗 2.企鹅");
        int typeNo = input.nextInt();
        switch (typeNo) {
            case 1:
                //接受用户键盘输入值
                System.out.println("请输入宠物的健康值:");
                int ghealth = input.nextInt();
                System.out.println("请输入宠物与主人的亲密度:");
                int glove = input.nextInt();
                System.out.println("请输入宠物的品种:");
                String strain = input.next();

                /*//创建狗狗对象,并为狗狗属性赋值
                Dog dog = new Dog();
                dog.setName(name);
                dog.health=-1000;
                dog.love=3;
                dog.name="多多";
                dog.strain="吉娃娃";
                dog.print();
                //dog.m1();*/
                //Pet pet=new Pet(); 类名 对象名=new 类名();
                //Person p=new Student();
                //Person p=new Teacher();
                //父类类型指向子类对象
                Pet dog=new Dog("狗蛋",30,23,"二哈");
                dog.print();

                System.out.println("*************************");
                //主人为狗狗看病
                master.cure(dog);
                System.out.println("**************************");
                dog.print();

                break;
            case 2:
                //接受用户键盘录入值
                System.out.println("请选择宠物的性别:1.Q妹 2.Q仔");
                int sexId = input.nextInt();
                String sex = (sexId == 1) ? "Q妹" : "Q仔";
                System.out.println("请输入宠物的健康值:");
                int qhealth = input.nextInt();
                System.out.println("请输入宠物和主人的亲密度:");
                int qlove = input.nextInt();

                /*//创建企鹅对象,并为企鹅属性赋值
                Penguin p = new Penguin();*/
            /*p.health=-1000;
            p.love=3;
            p.name="Q仔";
            p.sex="男";*/
                /*p.setName(name);
                p.setHealth(qhealth);
                p.setLove(qlove);
                p.setSex(sex);
                p.print();*/
                Pet p=new Penguin(name,qhealth,qlove,sex);

                System.out.println("*************************");
                //主人为企鹅看病
                master.cure(p);
                System.out.println("**************************");
                p.print();
                break;
            default:
                System.out.println("暂时没有这个类型的宠物,请在1或者2之间选择数值输入!");
                break;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值