我有一个课程Accommodation
如下:
class Accommodation {
int accommodationNo
String address1
String address2
String postcode
double valuation
double rent
int noRentPaymentsPerYe
}
班级Housing
扩展了Accommodation
:
class Housing : Accomodation {
String type
int noBedrooms
int noCarParkingSites
}
课程Flat
扩展Housing
:
class Flat : Housing {
double maintenanceCharge
}
创建参数为Flat
class的构造函数时,是否要将每个字段都包含在Accommodation
之内?参数类Flat
的构造函数的示例将受到高度赞赏。
答案 0 :(得分:1)
是的,您必须包含每个字段,并将其传递给构造函数链(这是字段的缩写,因为每个类只需要演示几个 - 您需要添加其他字段):< / p>
public abstract class Accommodation {
private int accommodationNo;
private String address1;
protected Accomodation(int acomNo, string addr1) {
this.accomodationNo = acomNo;
this.address1 = addr1;
}
}
public class Housing {
private String type;
private int noBedrooms;
public Housing(int acomNo, string addr1, string typ, int noBeds)
: base(acomNo, addr1) {
this.type = typ;
this.noBedrooms = noBeds;
}
}
public class Flat {
double maintenanceCharge;
public Flat(int acomNo, string addr1, string typ, int noBeds, double maintCharge)
: base(acomNo, addr1, typ, noBeds) {
this.maintenanceCharge = maintCharge;
}
}