14:子类想要有点不一样: 覆盖(override)

2021-03-27  本文已影响0人  爱学习的代代

上篇中我们提到需要实现一个功能:购买手机的时候,限制手机的单次购买次数(<= 5台)。
下面是使用继承的方式实现的:

package day09;

public class PhoneV2 extends MerchandiseWithConstructor{
    private double screenSize;
    private double cpuHZ;
    private double memoryG;
    private double storageG;
    private String brand;
    private String os;
//  给手机品类增加个数限制
    private static int MAX_BUY_COUNT = 5;


    public PhoneV2(int id, String name, int count, double soldPrice, double purchasingRrice,
            double screenSize, double cpuHZ, double memoryG, double storageG, String brand, String os) {
//      因为父类的属性不是公开的需要童工set方法来进行设置
        this.setId(id);
        this.setName(name);
        this.setCount(count);
        this.setSoldPrice(soldPrice);
        this.setPurchasingRrice(purchasingRrice);


        this.screenSize = screenSize;
        this.cpuHZ = cpuHZ;
        this.memoryG = memoryG;
        this.storageG = storageG;
        this.brand = brand;
        this.os = os;
    }
//  单独实现子类对于购买数量的限制
//  在子类中声明同样的方法(返回值、方法名、入参)

    public double buy(int count) {
        if (count > MAX_BUY_COUNT) {
            System.out.println("超出商品的单词购买上限,购买失败");
            return -1;
        }

        if (count > this.getCount()) {
            System.out.println("购买数超出商品最大库存,购买失败");
            return -2;
        }
//      扣减库存
        this.setCount(this.getCount() - count);
        double cost = count * this.getSoldPrice();
//      返回购买需要支付的花费
        System.out.println("购买手机所需要的花费为: " + cost + "\n");
        return cost;
    }
    public void describe() {
        System.out.println("手机配置信息如下:\n屏幕大小:" + this.screenSize + "英寸 \nCPU主频:" + this.cpuHZ + "GHz \n内存大小:" + this.memoryG + "G \n存储空间:" + storageG + "G \n品牌:" + this.brand + " \n操作系统:" + os);
    }

    public double getScreenSize() {
        return screenSize;
    }

    public void setScreenSize(double screenSize) {
        this.screenSize = screenSize;
    }

    public double getCpuHZ() {
        return cpuHZ;
    }

    public void setCpuHZ(double cpuHZ) {
        this.cpuHZ = cpuHZ;
    }

    public double getMemoryG() {
        return memoryG;
    }

    public void setMemoryG(double memoryG) {
        this.memoryG = memoryG;
    }

    public double getStorageG() {
        return storageG;
    }

    public void setStorageG(double storageG) {
        this.storageG = storageG;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

}

package day09;

public class TestPhoneV2 {
    public static void main(String[] args) {
        PhoneV2 v2 = new PhoneV2(200,"小米10",200,3999,3599,6,3.8,8.0,128.0,"小米","Android");

        v2.buy(3);
        v2.buy(100);

    }
}

二、几点说明:

  1. 使用继承的时候,可以获取的父类的属性和方法。
  2. 属性用于初始化子类的时候使用。因为父类的属性设置为了private,构造方法中使用的时候,需要通过set方法操作。
  3. 可以给自己添加自己的静态变量:private static int MAX_BUY_COUNT = 5;
  4. 覆盖父类的方法:即方法签名与父类保持一致。
  1. 继承不是直接把父类的方法直接拿来用,而是通过覆盖(override)来替换不适合子类的方法。
  2. 覆盖可以覆盖掉父类的方法:即同样的方法,不同的行为。这就是多态

三、思考:重复性代码解决。

phone类.png MerchandiseWithConstructor类

仔细观察一下,可以发现这里有重复代码:对于库存的最大限制判断。等后续再解决。

上一篇 下一篇

猜你喜欢

热点阅读