【发布时间】:2020-01-11 17:55:46
【问题描述】:
所以我正在为班级做作业,我们需要创建 3 个班级: Person、Customer 和 PreferredCustomer,按该顺序排列层次结构。
就个人而言,我有以下构造函数:
public Person() {
this.name = "";
this.address = "";
this.phoneNumber = "";
}
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
在客户中:
public Customer(boolean isOnMailingList, int customerId) {
this.isOnMailingList = isOnMailingList;
this.customerId = customerId;
}
在优惠客户中:
public PreferredCustomer(boolean isOnMailingList, int customerId, double purchaseAmount) {
super(isOnMailingList, customerId);
this.purchaseAmount = purchaseAmount;
this.discountPercentage = calculateDiscountPercentage();
}
所以我的问题主要涉及子类中的构造函数。我应该像在 PreferredCustomer 中那样接受子类的构造函数中的父成员,还是应该在创建对象实例时使用 setter?我唯一担心的是子类的构造函数将有很长的参数列表。你在这里有什么想法?谢谢!
【问题讨论】:
-
我对你的问题有点困惑。如果您的父类除了参数构造函数之外没有任何其他构造函数,那么您将始终必须在子构造函数中声明
super(...),没有办法。