13 多态

简介: 13 多态

配套视频教程


本文B站配套视频教程


问题

宠物饿了,需要主人给宠物喂食

  • 不同宠物吃的东西不一样,狗吃骨头,企鹅吃鱼
  • 不同宠物恢复后体力值不一样
  • 狗狗类
    增加狗狗吃食的方法
  • 企鹅类
    增加企鹅吃食的方法
  • 创建主人类
    编写给狗狗喂食的方法
    编写给企鹅喂食的方法
  • 编写测试方法
    调用主人类给狗狗喂的方法
    调用主人类给企鹅喂的方法

宠物父类

public abstract class Pet {
    private String name = "无名氏";// 昵称
    private int health = 100;// 健康值
    private int love = 0;// 亲密度
    /**
     * 抽象方法eat(),负责宠物吃饭功能。
     */
    public abstract void eat();
    /**
     * 有参构造方法。
     * @param name  昵称
     */
    public Pet(){
    }
    public Pet(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setHealth(int health) {
        this.health = health;
    }
    public void setLove(int love) {
        this.love = love;
    }
    public String getName() {
        return name;
    }
    public int getHealth() {
        return health;
    }
    public int getLove() {
        return love;
    }
    /**
     * 输出宠物信息。
     */
    public void print() {
        System.out.println("宠物的自白:\n我的名字叫" + this.name + 
                ",健康值是" + this.health + ",和主人的亲密度是"
                + this.love + "。");
    }   
}

狗类

public class Dog extends Pet {
    private String strain;// 品种
    /**
     * 有参构造方法。
     * @param name   昵称
     * @param strain   品种
     */
    public Dog(String name, String strain) {
        super(name); 
        this.strain = strain;
    }
    public String getStrain() {
        return strain;
    }
    /**
     * 重写父类的print方法。
     */
    public void print(){
        super.print(); //调用父类的print方法
        System.out.println("我是一只 " + this.strain + "。");
    }
    /**
     * 实现吃食方法。 
     */
    public void eat() {
        if(getHealth()>=100){
            System.out.println("狗狗"+this.getName() +"吃饱了,不需要喂食了!");
        }else{
            this.setHealth(this.getHealth()+3);
            System.out.println("狗狗"+this.getName() + "吃饱啦!健康值增加3。");
        }
    }
}

企鹅类

public class Penguin extends Pet {
    private String sex;// 性别
    /**
     * 有参构造方法。
     * @param name 昵称
     * @param sex 性别
     */
    public Penguin(String name, String sex) {
        super(name);
        this.sex = sex;
    }
    public String getSex() {
        return sex;
    }
    /**
     * 重写父类的print方法。
     */
    public void print() {
        super.print();
        System.out.println("性别是 " + this.sex + "。");
    }
    /**
     * 实现吃食方法。 
     */
    public void eat() {
        if(getHealth()>=100){
            System.out.println("企鹅"+this.getName() +"吃饱了,不需要喂食了!");
        }else{
            this.setHealth(this.getHealth()+5);
            System.out.println("企鹅"+this.getName() + "吃饱啦!健康值增加3。");
        }
    }
}

主人类

public class Master {
    private String name = "";// 主人名字
    private int money = 0; // 元宝数
    /**
     * 有参构造方法。
     * @param name 主人名字
     * @param money 元宝数
     */
    public Master(String name, int money) {
        this.name = name;
        this.money = money;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getMoney() {
        return money;
    }
    public String getName() {
        return name;
    }
    /**
     * 主人给Dog喂食。
     */
    public void feed(Dog dog) {
        dog.eat();
    }
    /**
     * 主人给Penguin喂食。
     */
    public void feed(Penguin pgn) {
        pgn.eat();
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog("欧欧", "雪娜瑞");
        dog.setHealth(80); //设置健康值,以便正常喂食
        Penguin pgn = new Penguin("楠楠", "Q妹");
        Master master=new Master("王先生",100);
        master.feed(dog);//主人给狗狗喂食
        master.feed(pgn);//主人给企鹅喂食
        pgn.setHealth(80); //设置健康值,以便正常喂食
        master.feed(pgn);//主人再次给企鹅喂食
    }
}


如果再领养新种类XXX的宠物,就需要给XXX喂食,怎么办?


  • 添加XXX类,继承Pet类,实现吃食方法
  • 修改Master类,添加给XXX喂食的方法

public class Master {
       public void feed( Dog dog ) {
              dog.eat();
       } 
    public void feed( Penguin pgn ) {
               pgn.eat();
        }
    public void feed( XXX xxx ) {
               xxx.eat();
        }
        … …
}

频繁修改代码,代码可扩展性、可维护性差,如何优化?

使用多态优化设计

参数都是Pet类的子类

可否使用一个feed(Pet pet)实现对所有宠物的喂食?

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

使用多态实现思路

  • 编写父类
  • 编写子类,子类重写父类方法
  • 运行时,使用父类的类型,子类的对象
    Pet pet = new Dog();

使用父类作为方法形参实现多态

使用多态优化主人给宠物喂食

public class Master {
//使用父类作为方法形参 
    public void feed( Pet pet ) {
           pet.eat();         
    }
}

测试类的改变

Pet pet = new Dog();
Master master = new Master();
master.feed( pet );//同一种操作方式,不同的操作对象

练习

使用多态实现喂养宠物功能

增加宠物猫并喂食,其健康值增加4


计算一次租赁多辆汽车的总租金


在继承章节汽车租赁系统的基础上,实现计算多种车辆总租金的功能

现在有客户租用

2辆宝马

1辆别克商务舱

1辆金龙(34)座

租5天共多少租金?


解题思路


1、创建车的对象,放在数组中

MotoVehile[] motos = new MotoVehile[4];
motos[0] = new Car("宝马550i","京NY28588");
motos[1] = new Car("宝马550i","京NNN328");
motos[2] = new Car("别克林荫大道","京NY28588");
motos[3] = new Bus("金龙",34);

2 循环调用calcRent()方法,计算总租金

public int calcTotalRent(MotoVehile[] motos,int days){
       int totalRent = 0; 
       for(int i=0;i<motos.length;++i){
            totalRent += motos[i].calRent(days);
       } 
       return totalRent;
}

假设新增了一种对外出租的车辆类型

新购置了卡车,根据吨位,租金每吨每天50

对系统进行扩展,计算汽车租赁的总租金

解题思路

1.创建卡车类,实现calcRent ()方法

2.修改统计租金代码

public abstract class MotoVehicle {
    private String no;//车牌号
    private String brand;//品牌
    abstract int calRent(int days);//计算租金
    public MotoVehicle()
    {
    }
    public MotoVehicle(String no, String brand)
    {
        this.no = no;
        this.brand = brand;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
}

public class Car extends MotoVehicle{
    public Car(String no, String brand)
    {
        super(no,brand);
    }
    @Override
    int calRent(int days) {
        if(getBrand().equals("宝马"))
        {
            return 500 * days;
        }
        else
        {
            return 600 * days;
        }
    }
}

public class Kache extends MotoVehicle {
    private int dunwei;//吨位
    public Kache(int dunwei)
    {
        this.dunwei = dunwei;
    }
    @Override
    int calRent(int days) {
        return 50*dunwei*days;
    }
}

不用多态的实现方式

Car[] cars = new Car[3];
        cars[0] = new Car("辽A12345","宝马");
        cars[1] = new Car("辽B22222","别克");
        cars[2] = new Car("辽A62222","奔驰");
        Kache[] kaches = new Kache[2];
        kaches[0] = new Kache(1);//150
        kaches[1] = new Kache(4);//600
        int total = 0;
        for(int i = 0; i < cars.length; i++)
        {
            total += cars[i].calRent(3);
        }
        for(int i = 0; i < kaches.length; i++)
        {
            total += kaches[i].calRent(3);
        }
        System.out.println("总租金" + total);//5850

使用多态实现

MotoVehicle[] motoVehicles = new MotoVehicle[5];
        motoVehicles[0] = new Car("辽A12345","宝马");
        motoVehicles[1] = new Car("辽B22222","别克");
        motoVehicles[2] = new Car("辽A62222","奔驰");
        motoVehicles[3] = new Kache(1);
        motoVehicles[4] = new Kache(4);
        int total = 0;
        for(int i = 0; i < motoVehicles.length; i++)
        {
            total += motoVehicles[i].calRent(3);
        }
        System.out.println("总租金" + total);

一个练习

工资支付系统

定义一个Employee抽象基类(name)

公司有以下几种员工:

开发人员:工资计算方式,每月固定工资

销售人员:底薪+提成

硬件工程师:生产零件,每个50元

小时工:按工作时间付工资,每小时30元

主类(测试类)

创建不同类型的6名员工对象,计算他们应付的月工资之和

public abstract class Employee {
    private String name;
    public Employee(String name)
    {
        this.name = "@" + name + "@" ;
    }
    protected abstract int paySalary();
}

//开发人员
public class Developer extends Employee{
    private int salary;
    public Developer(String name, int salary)
    {
        super(name);
        this.salary = salary;
    }
    @Override//方法重写注解
    protected int paySalary() {
        return salary;
    }
}

//销售人员
public class SaleMan extends Employee{
    private int salary;
    private int comm;//提成
    public SaleMan(String name, int salary, int comm)
    {
        super(name);
        this.salary = salary;
        this.comm = comm;
    }
    @Override
    protected int paySalary() {
        return salary + comm;
    }
}

//硬件工程师
public class HardwareEngineer extends Employee {
    private int productCount;//每月生产的零件数量
    public HardwareEngineer(String name, int productCount)
    {
        super(name);
        this.productCount = productCount;
    }
    @Override
    protected int paySalary() {
        return 50*productCount;
    }
}

//小时工
public class HourlyWorker extends Employee{
    private int hours;//这个月工作了多少小时
    public HourlyWorker(String name, int hours)
    {
        super(name);
        this.hours = hours;
    }
    @Override
    protected int paySalary() {
        return 30*hours;
    }
}

public class Boss extends Employee {
    public Boss(String name)
    {
        super(name);
    }
    @Override
    protected int paySalary() {
        return 200000;
    }
}

public class Main {
    public static void main(String[] args) {
    // write your code here
        Employee[] employees = new Employee[6];
        employees[0] = new Developer("张二",5000);
        employees[1] = new Developer("张三",5000);
        employees[2] = new SaleMan("李四", 2000, 500);
        employees[3] = new HardwareEngineer("王五", 200);
        employees[4] = new HourlyWorker("冯六",200);
        employees[5] = new Boss("赵本山");
        //计算总工资
        int total = 0;
        for(int i = 0; i < employees.length; i++)
        {
            total += employees[i].paySalary();
        }
        System.out.println("总共需要支付" + total);
    }
}


目录
相关文章
|
3天前
|
存储 JavaScript 前端开发
JavaScript基础
本节讲解JavaScript基础核心知识:涵盖值类型与引用类型区别、typeof检测类型及局限性、===与==差异及应用场景、内置函数与对象、原型链五规则、属性查找机制、instanceof原理,以及this指向和箭头函数中this的绑定时机。重点突出类型判断、原型继承与this机制,助力深入理解JS面向对象机制。(238字)
|
1天前
|
云安全 人工智能 安全
阿里云2026云上安全健康体检正式开启
新年启程,来为云上环境做一次“深度体检”
1470 6
|
3天前
|
安全 数据可视化 网络安全
安全无小事|阿里云先知众测,为企业筑牢防线
专为企业打造的漏洞信息收集平台
1305 2
|
3天前
|
缓存 算法 关系型数据库
深入浅出分布式 ID 生成方案:从原理到业界主流实现
本文深入探讨分布式ID的生成原理与主流解决方案,解析百度UidGenerator、滴滴TinyID及美团Leaf的核心设计,涵盖Snowflake算法、号段模式与双Buffer优化,助你掌握高并发下全局唯一ID的实现精髓。
319 160
|
3天前
|
人工智能 自然语言处理 API
n8n:流程自动化、智能化利器
流程自动化助你在重复的业务流程中节省时间,可通过自然语言直接创建工作流啦。
366 4
n8n:流程自动化、智能化利器
|
12天前
|
机器学习/深度学习 安全 API
MAI-UI 开源:通用 GUI 智能体基座登顶 SOTA!
MAI-UI是通义实验室推出的全尺寸GUI智能体基座模型,原生集成用户交互、MCP工具调用与端云协同能力。支持跨App操作、模糊语义理解与主动提问澄清,通过大规模在线强化学习实现复杂任务自动化,在出行、办公等高频场景中表现卓越,已登顶ScreenSpot-Pro、MobileWorld等多项SOTA评测。
1479 7
|
5天前
|
人工智能 API 开发工具
Skills比MCP更重要?更省钱的多!Python大佬这观点老金测了一周终于懂了
加我进AI学习群,公众号右下角“联系方式”。文末有老金开源知识库·全免费。本文详解Claude Skills为何比MCP更轻量高效:极简配置、按需加载、省90% token,适合多数场景。MCP仍适用于复杂集成,但日常任务首选Skills。推荐先用SKILL.md解决,再考虑协议。附实测对比与配置建议,助你提升效率,节省精力。关注老金,一起玩转AI工具。
|
2天前
|
Linux 数据库
Linux 环境 Polardb-X 数据库 单机版 rpm 包 安装教程
本文介绍在CentOS 7.9环境下安装PolarDB-X单机版数据库的完整流程,涵盖系统环境准备、本地Yum源配置、RPM包安装、用户与目录初始化、依赖库解决、数据库启动及客户端连接等步骤,助您快速部署运行PolarDB-X。
228 1
Linux 环境 Polardb-X 数据库 单机版 rpm 包 安装教程
|
12天前
|
人工智能 Rust 运维
这个神器让你白嫖ClaudeOpus 4.5,Gemini 3!还能接Claude Code等任意平台
加我进AI讨论学习群,公众号右下角“联系方式”文末有老金的 开源知识库地址·全免费
1343 17
|
3天前
|
自然语言处理 监控 测试技术
互联网大厂“黑话”完全破译指南
互联网大厂黑话太多听不懂?本文整理了一份“保姆级”职场黑话词典,涵盖PRD、A/B测试、WLB、埋点、灰度发布等高频术语,用大白话+生活化类比,帮你快速听懂同事在聊什么。非技术岗也能轻松理解,建议收藏防踩坑。
277 161

热门文章

最新文章