Java 问题:一个子类的构造函数中的计算影响另一个子类实例的字段

Java Problem: Calculations in one subclass' constructor affecting fields of another subclass' instance

我有两个摘要 classes,即 Medicine 和 Prescription。所有代码都可以在https://codeshare.io/aVAdr3找到 这两个classes有子classes,class层级图如下:

和...

药品java档案:

abstract class Medicine {
    public String name;
    public int price;

    public Medicine (String name, int price) {

    this.name = name;
    this.price = price;
}

public int getPrice () {
    return price;
} 

public void setPrice (int newPrice){
        price = newPrice;
    }
}

class commonDrug extends Medicine {
    public commonDrug (String name, int price) {
        super(name, price);
    }
}

处方java文件:

abstract class Prescription {
    protected Medicine med;

    public Prescription(Medicine med) {
        this.med = med;
    }
}


class bluePrescription extends Prescription {
    public bluePrescription (Medicine med) {
        super(med);
        System.out.println(med.getPrice()+ "<-- Price for bluePrescription, it should be 30, but the calculations in pPrescriptions affect it.");
    }
}

class whitePrescription extends Prescription {
    public whitePrescription (Medicine med) {
        super(med);
        
    }
}


class pPrescription extends whitePrescription {
    public pPrescription (Medicine med) {
        super(med);
        System.out.println(med.getPrice()+ "<-- Price before calculation for pPrescription");
        
        //Calculations
        int priceWithDiscount;
        if (med.getPrice()<=20) {priceWithDiscount=0;}
        else {priceWithDiscount= med.getPrice()-20;}
        med.setPrice(priceWithDiscount);

        System.out.println(med.getPrice()+ "<-- Price after calculation for pPrescription");
    }

}

测试程序如下:

class TestProgram {
    public static void main (String[] args) {
        //Medicine object
        commonDrug drug1 = new commonDrug("Paracetamol", 30);
    
        //Prescription objects:
        pPrescription prescription1 = new pPrescription(drug1);
        bluePrescription prescription2 = new bluePrescription(drug1);
    }
}

当你 运行 测试程序时,你会在终端中得到这个:

30<-- Price before calculation for pPrescription
10<-- Price after calculation for pPrescription
10<-- Price for bluePrescription, it should be 30, but the calculations in pPrescriptions affect it.

我已经尝试解决这个问题好几个小时了,我不知道如何在不影响 bluePrescription 实例的情况下在 pPrescription 构造函数中执行计算。为什么会这样? pPrescription 是 whitePrescriptions 而不是 bluePrescriptions 的子class。无论如何,class 的实例是完全独立的,getPrice 和 setPrice 不是静态的,那么为什么使用它们会影响 Medicine 的所有实例?

why is using them affecting all the instances of Medicine?

您的代码中只有一次 Medicine 实例。

您将相同的对象,即 drug1 传递给 pPrescriptionbluePrescription class 构造函数。

因为只有一个对象 (drug1) 被传递给两个 classes,如果任何 class 修改它,更改将反映在您引用该对象的所有地方。

解决此问题的一种方法是不保存折扣价,而是在需要时使用 pPrescription class.

中的方法计算它
class pPrescription extends whitePrescription {

    ...

    public int getDiscountedPrice() {
        return med.getPrice() <= 20 ? 0 : med.getPrice() - 20;
    }    
}

旁注: Class 名称应以大写字母开头。