2.35.子类对象里藏着一个父类对象

l 另一种解决问题的思路:组合
看例程:学习使用组合如何解决问题

l 继承和组合的区别
看例程:初步了解组合和继承的区别

public class UsePhoneExtendsMerchandise {
    public static void main(String[] args) {
        PhoneExtendsMerchandise phone  = new PhoneExtendsMerchandise(
            "手机001","Phone001",100, 1999, 999,
            4.5,3.5,4,128,"索尼","安卓"
        );

        phone.describePhone();

        phone.buy(100);
    }
}
public class UsePhoneHasAMerchandise {
    public static void main(String[] args) {
        MerchandiseV2 merchandise = new MerchandiseV2(
            "手机001", "Phone001", 100, 1999, 999);
        PhoneHasAMerchandise phone = new PhoneHasAMerchandise(
            4.5, 3.5, 4, 128, "索尼", "安卓", merchandise);

        phone.describePhone();

        // TODO 所有和商品相关的操作,我们要先从Phone里获得商品的引用,然后再通过这个引用操作商品
        phone.getMerchandise().describe();

        // TODO 如果我们想要让手机返回不一样的商品名字,比如加上手机厂商和型号,其实可以做到,但是不容易。
        // TODO 我们可以在每次修改手机的厂商和型号的时候,都去set一下商品的名字。繁琐,但是做得到
        phone.getMerchandise().getName();

        // TODO 如果要限制购买的数量不超过5怎么办?来移步继承
        phone.getMerchandise().buy(100);

    }
}
public class LittleSuperMarket {
    public String superMarketName;
    public String address;
    public int parkingCount;
    public double incomingSum;
    public MerchandiseV2[] merchandises;
    public int[] merchandiseSold;

    /**
     * 初始化小超市
     *
     * @param superMarketName
     * @param address
     * @param parkingCount
     * @param merchandiseCount 商品种类数
     * @param count            每种商品缺省库存
     */
    public LittleSuperMarket(String superMarketName, String address, int parkingCount,
                             int merchandiseCount, int count) {
        this.superMarketName = superMarketName;
        this.address = address;
        this.parkingCount = parkingCount;

        merchandises = new MerchandiseV2[merchandiseCount];
        for (int i = 0; i < merchandises.length; i++) {
            double purchasePrice = Math.random() * 200;
            // 创建并给商品的属性赋值
            MerchandiseV2 m = new MerchandiseV2(
                "商品" + i,
                "ID" + i,
                count,
                purchasePrice * (1 + Math.random()),
                purchasePrice
            );
            // 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
            merchandises[i] = m;
        }
        merchandiseSold = new int[merchandises.length];
    }

    // 简单的访问成员变量

    public String getSuperMarketName() {
        return superMarketName;
    }

    public String getAddress() {
        return address;
    }

    public int getParkingCount() {
        return parkingCount;
    }

    public double getIncomingSum() {
        return incomingSum;
    }

    public void setSuperMarketName(String superMarketName) {
        this.superMarketName = superMarketName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setParkingCount(int parkingCount) {
        this.parkingCount = parkingCount;
    }

    public void setIncomingSum(double incomingSum) {
        this.incomingSum = incomingSum;
    }

    public void setMerchandises(MerchandiseV2[] merchandises) {
        this.merchandises = merchandises;
    }

    public void setMerchandiseSold(int[] merchandiseSold) {
        this.merchandiseSold = merchandiseSold;
    }

    // 一些特殊的逻辑

    /**
     * 得到利润最高的商品
     *
     * @return
     */
    public MerchandiseV2 getBiggestProfitMerchandise() {
        MerchandiseV2 curr = null;
        for (int i = 0; i < merchandises.length; i++) {
            MerchandiseV2 m = merchandises[i];
            // 这个逻辑有问题吗?相同的利润怎么判断?
            if (curr == null || curr.calculateProfit() < m.calculateProfit()) {
                curr = m;
            }
        }
        return curr;
    }

    /**
     * 根据索引获取商品
     *
     * @param merchandiseIndex
     * @return
     */
    public MerchandiseV2 getMerchandiseOf(int merchandiseIndex) {
        if (merchandiseIndex < 0 || merchandiseIndex >= merchandises.length) {
            return null;
        }
        return merchandises[merchandiseIndex];
    }

    /**
     * 赚钱
     *
     * @param toBeAdded
     */
    public void addIncomingSum(double toBeAdded) {
        this.incomingSum += toBeAdded;
    }

    /**
     * 花钱
     *
     * @param toBeSpent
     * @return
     */
    public boolean spendMoney(double toBeSpent) {
        if (toBeSpent > incomingSum) {
            return false;
        }
        incomingSum -= toBeSpent;
        return true;
    }

}
public class MerchandiseV2 {

    public String name;
    public String id;
    public int count;
    public double soldPrice;
    public double purchasePrice;


    public MerchandiseV2(String name, String id, int count, double soldPrice, double purchasePrice) {
        this.name = name;
        this.id = id;
        this.count = count;
        this.soldPrice = soldPrice;
        this.purchasePrice = purchasePrice;
        // soldPrice = 9/0;
    }

    public MerchandiseV2(String name, String id, int count, double soldPrice) {
        // double purPrice = soldPrice * 0.8;
        // this(name, id, count, soldPrice, purchasePrice);
        this(name, id, count, soldPrice, soldPrice * 0.8);
        // double purPrice = soldPrice * 0.8;
    }

    public MerchandiseV2() {
        this("无名", "000", 0, 1, 1.1);

    }

    public void describe() {
        System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
            + "。商品进价是" + purchasePrice + "。商品库存量是" + count +
            "。销售一个的毛利润是" + (soldPrice - purchasePrice));
    }

    public double calculateProfit() {
        double profit = soldPrice - purchasePrice;
//        if(profit <= 0){
//            return 0;
//        }
        return profit;
    }

    public double buy(int count) {
        if (this.count < count) {
            return -1;
        }
        this.count -= count;

        return count * soldPrice;
    }


    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getSoldPrice() {
        return soldPrice;
    }

    public void setSoldPrice(double soldPrice) {
        this.soldPrice = soldPrice;
    }

    public double getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(double purchasePrice) {
        this.purchasePrice = purchasePrice;
    }
}
// >> TODO 继承,其实表达的是一种"is-a"的关系,也就是说,在你用类构造的世界中,"子类是父类的一种特殊类别"

// >> TODO 组合和继承,是拿到一个问题,设计相应的Java类的时候,不得不面对的来自灵魂的拷问
//    TODO "XX到底是YY的一种,还是只是组合了YY?","手机到底是手电筒的一种,还是组合了一个可以当手电的闪光灯?"


// TODO 在组合的情况下,怎么限制一次只能买五个手机呢?
// TODO 1)首先不能修改MerchandiseV2这个类,否则你会限制所有商品一次购买的数量
// TODO 2)其次,在现实情况下,这个类可能根本不受你控制,你无权修改其代码
// TODO 3)在每次调用buy方法的地方做限制,是不行的,
//      TODO - 你无法控制别人怎么用你的类,
//      TODO - 而且会面临到处复制代码的糟糕情况,
//      TODO - 如果说限制改成10个,所有复制的代码都要改,程序员都应该很懒,这不是一个程序员该做的事情
// TODO 4)在只能修改手机类的情况下,我们可以提供一个buyPhone的方法,实现限制购买数量的逻辑。
// TODO    但是这样并不能阻止别人像下面这样调用merchandise的buy方法,这个方法是会修改库存的,所以还是无法硬性的限制一次性购买手机的数量

// TODO 我们来理清一下自己的核心诉求:针对手机,限制一次性购买的数量。必须限制死,必须不影响别的商品,必须只能改手机类的代码
// TODO 这时候,组合就无能为力了,继承可以发挥其应有的作用。


// >> TODO 继承不是组合,继承也不只是为了能简单的拿来父类的属性和方法。如果仅仅如此,原封不动的拿来主义,组合也能做到。
//    TODO 继承也不是通过组合的方式来实现的。和组合相比,继承更像是"融合"
//    TODO 所谓融合,即合二为一,可以互相影响。父类影响子类没问题,子类怎么影响父类呢?如何限制手机一次只能最多买五个?

public class PhoneExtendsMerchandise extends MerchandiseV2 {

    // 给Phone增加新的属性和方法
    private double screenSize;
    private double cpuHZ;
    private int memoryG;
    private int storageG;
    private String brand;
    private String os;

    public PhoneExtendsMerchandise(
        String name, String id, int count, double soldPrice, double purchasePrice,
        double screenSize, double cpuHZ, int memoryG, int storageG, String brand, String os
    ) {

        this.screenSize = screenSize;
        this.cpuHZ = cpuHZ;
        this.memoryG = memoryG;
        this.storageG = storageG;
        this.brand = brand;
        this.os = os;

        this.setName(name);
        this.setId(id);
        this.setCount(count);
        this.setSoldPrice(soldPrice);
        this.setPurchasePrice(purchasePrice);
    }

    public void describePhone() {
        System.out.println("此手机商品属性如下");
        describe();
        System.out.println("手机厂商为" + brand + ";系统为" + os + ";硬件配置如下:\n" +
            "屏幕:" + screenSize + "寸\n" +
            "cpu主频" + cpuHZ + " GHz\n" +
            "内存" + memoryG + "Gb\n" +
            "存储空间" + storageG + "Gb\n");
    }


    public boolean meetCondition() {
        return true;
    }

    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 int getMemoryG() {
        return memoryG;
    }

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

    public int getStorageG() {
        return storageG;
    }

    public void setStorageG(int 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;
    }
}
public class PhoneHasAMerchandise {


    // 给Phone增加新的属性和方法

    private double screenSize;
    private double cpuHZ;
    private int memoryG;
    private int storageG;
    private String brand;
    private String os;

    // >> TODO 除了继承,我们也可以优化上节中使用的拷贝所有代码的方法,在Phone中添加一个商品的引用
    //    TODO 这样,我们也是可以实现类似的功能。
    // >> TODO 这种在自己的类里,使用别的类的两类之间关系,我们叫做"has-a",也称为组合
    //    TODO 具体到我们这个例子,在我们用类构造的世界中,"Phone中有一个商品,但是Phone本身不是商品(划重点!)"
    private MerchandiseV2 merchandise;

    public PhoneHasAMerchandise(double screenSize, double cpuHZ, int memoryG, int storageG,
                                String brand, String os, MerchandiseV2 merchandise) {

        this.screenSize = screenSize;
        this.cpuHZ = cpuHZ;
        this.memoryG = memoryG;
        this.storageG = storageG;
        this.brand = brand;
        this.os = os;
        this.merchandise = merchandise;
    }


    public void describePhone() {
        System.out.println("此手机商品属性如下");
        merchandise.describe();
        System.out.println("手机厂商为" + brand + ";系统为" + os + ";硬件配置如下:\n" +
            "屏幕:" + screenSize + "寸\n" +
            "cpu主频" + cpuHZ + " GHz\n" +
            "内存" + memoryG + "Gb\n" +
            "存储空间" + storageG + "Gb\n");

    }


    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 int getMemoryG() {
        return memoryG;
    }

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

    public int getStorageG() {
        return storageG;
    }

    public void setStorageG(int 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;
    }

    public MerchandiseV2 getMerchandise() {
        return merchandise;
    }

    public void setMerchandise(MerchandiseV2 merchandise) {
        this.merchandise = merchandise;
    }
}

继承,其实表达的是一种"is-a"的关系,也就是说,在你用类构造的世界中,“子类是父类的一种特殊类别”

组合和继承,是拿到一个问题,设计相应的Java类的时候,不得不面对的来自灵魂的拷问
“XX到底是YY的一种,还是只是组合了YY?”,“手机到底是手电筒的一种,还是组合了一个可以当手电的闪光灯?”

在组合的情况下,怎么限制一次只能买五个手机呢?
1)首先不能修改MerchandiseV2这个类,否则你会限制所有商品一次购买的数量
2)其次,在现实情况下,这个类可能根本不受你控制,你无权修改其代码
3)在每次调用buy方法的地方做限制,是不行的,

  • 你无法控制别人怎么用你的类,
  • 而且会面临到处复制代码的糟糕情况,
  • 如果说限制改成10个,所有复制的代码都要改,程序员都应该很懒,这不是一个程序员该做的事情

4)在只能修改手机类的情况下,我们可以提供一个buyPhone的方法,实现限制购买数量的逻辑。
但是这样并不能阻止别人像下面这样调用merchandise的buy方法,这个方法是会修改库存的,所以还是无法硬性的限制一次性购买手机的数量

我们来理清一下自己的核心诉求:针对手机,限制一次性购买的数量。必须限制死,必须不影响别的商品,必须只能改手机类的代码
这时候,组合就无能为力了,继承可以发挥其应有的作用。

继承不是组合,继承也不只是为了能简单的拿来父类的属性和方法。如果仅仅如此,原封不动的拿来主义,组合也能做到。
继承也不是通过组合的方式来实现的。和组合相比,继承更像是"融合"
所谓融合,即合二为一,可以互相影响。父类影响子类没问题,子类怎么影响父类呢?如何限制手机一次只能最多买五个?

除了继承,我们也可以优化上节中使用的拷贝所有代码的方法,在Phone中添加一个商品的引用
这样,我们也是可以实现类似的功能。
这种在自己的类里,使用别的类的两类之间关系,我们叫做"has-a",也称为组合
具体到我们这个例子,在我们用类构造的世界中,“Phone中有一个商品,但是Phone本身不是商品(划重点!)”

发布了57 篇原创文章 · 获赞 0 · 访问量 503

猜你喜欢

转载自blog.csdn.net/weixin_45471415/article/details/104801358