Java问题:一个子类中的计算';影响另一个子类的字段的构造函数';实例
我有两个抽象类,即医学和处方。所有代码都可以在这两个类中找到,都有子类,类的层次结构图如下:Java问题:一个子类中的计算';影响另一个子类的字段的构造函数';实例,java,object,constructor,instance,subclass,Java,Object,Constructor,Instance,Subclass,我有两个抽象类,即医学和处方。所有代码都可以在这两个类中找到,都有子类,类的层次结构图如下: class TestProgram { public static void main (String[] args) { //Medicine object commonDrug drug1 = new commonDrug("Paracetamol", 30); //Prescription objects:
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);
}
}
而且
medicine 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);
}
}
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");
}
}
处方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);
}
}
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");
}
}
当你运行测试程序时,你会在终端上看到:
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.
30
为什么使用它们会影响所有的医学实例
您的代码中只有一个医学
实例
您将相同的对象,即drug1
传递给pDescription
和bluePrescription
类构造函数
由于只有一个对象(drug1
)被传递给这两个类,如果任何类修改它,更改将反映在引用该对象的任何地方
解决此问题的一种方法是不保存折扣价格,只要在需要时使用pDescription
类中的方法计算折扣价格即可
class pPrescription extends whitePrescription {
...
public int getDiscountedPrice() {
return med.getPrice() <= 20 ? 0 : med.getPrice() - 20;
}
}
类描述扩展了{
...
public int getDiscountedPrice(){
return med.getPrice()
为什么使用它们会影响所有的医学实例
您的代码中只有一个医学
实例
您将相同的对象,即drug1
传递给pDescription
和bluePrescription
类构造函数
由于只有一个对象(drug1
)被传递给这两个类,如果任何类修改它,更改将反映在引用该对象的任何地方
解决此问题的一种方法是不保存折扣价格,只要在需要时使用pDescription
类中的方法计算折扣价格即可
class pPrescription extends whitePrescription {
...
public int getDiscountedPrice() {
return med.getPrice() <= 20 ? 0 : med.getPrice() - 20;
}
}
类描述扩展了{
...
public int getDiscountedPrice(){
return med.getPrice()通过调用传递的Medicine-med
对象上的setter来显式修改该对象由于您只创建了一个medicine对象,因此对该对象的任何更改都将反映在任何位置。您不应该在构造函数中修改medicine
。也许您的处方应该有一个getPrice
方法返回药物/处方的价格,但绝对不应该修改现有的gMedicine
object。@噢,上帝啊,谢谢你,我明白了。你通过调用传递的Medicine-med
对象上的setter来显式修改它med.setPrice(priceWithDiscount)由于您只创建了一个medicine对象,因此对该对象的任何更改都将反映在任何位置。您不应该在构造函数中修改medicine
。也许您的处方应该有一个getPrice
方法返回药物/处方的价格,但绝对不应该修改现有的gMedicine
object.@噢,上帝啊,谢谢你,我明白了。