继承让我们假设我有一个抽象类:从抽象类
public abstract class Customer {
private int price;
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public abstract void pay(int n);
public abstract void occupySpace();
}
我也有继承它两类:
HandicappedCustomer
,在这里我想设置的价格为0RegularCustomer
,在这里我想将价格定为100
现在,如果我创建了这些类中的每个类的实例并调用getPrice()
,则两者都返回0。当我为每个相应的课程实例调用getPrice()
时,使其返回0和100的最简单方法是什么?
当我尝试在RegularCustomer
类中调用setPrice(100)
时,我发现它不起作用。说白了,我想在我的主下面的代码:
Customer a = new HandicappedCustomer();
Customer b = new RegularCustomer();
System.out.println(a.getPrice());
System.out.println(b.getPrice());
并使其返回:
向我们展示您的代码,适用于所有课程和您的主要课程。 – MrSimpleMind