1 回答
TA贡献1802条经验 获得超5个赞
乍一看,一切似乎都很好。我希望你在编译之前保存了文件。
既然你提到你不明白什么是重载构造函数,我会尽力解释一下。
重载的构造函数具有相同的构造函数名称,但它在以下方面与其他构造函数不同 -
它有不同数量的正式参数
构造函数形参类型顺序不同
这是一个例子 -
public class Customer {
private String firstName;
private String lastName;
private int phoneNumber;
public Customer() {
// default constructor
}
public Customer(String firstName) {
this.firstName = firstName;
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(String firstName, String lastName, int phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
public Customer(int phoneNumber, String firstName, String lastName) {
this.phoneNumber = phoneNumber;
this.firstName = firstName;
this.lastName = lastName;
}
// This is not an overloaded constructor as there is already a constructor of type
// Customer(String, String)
// public Customer(String lastName, String firstName) {
// this.lastName = lastName;
// this.firstName = firstName;
// }
}
添加回答
举报