我的 Person 类中有一个 (boolean)hasDriverLicence 变量。我创建了 getter 和 setter 方法,并在 person 构造函数中使用了 hasDriverLicence,但我的 Eclipse 说“未使用字段 Person.hasDriverLicence 的值”。这是代码:
public Person(int id, String firstName, String lastName, String gender, Calendar birthDate, String maritalStatus,
String hasDriverLicence) throws Exception {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
setGender(gender);
setMaritalStatus(maritalStatus);
setHasDriverLicence(hasDriverLicence);
这是getter和setter:
public void setHasDriverLicence(String hasDriverLicence) throws Exception {
if (!(hasDriverLicence.equalsIgnoreCase("Yes")) && !(hasDriverLicence.equalsIgnoreCase("No")))
throw new Exception("Wrong input, please type Yes or No");
if (hasDriverLicence.equalsIgnoreCase("Yes")) {
this.hasDriverLicence = true;
}
else if (hasDriverLicence.equalsIgnoreCase("No")) {
this.hasDriverLicence = false;
}
}
public String getHasDriverLicence() {
if (this.hasDriverLicence = true)
return "Yes";
if (this.hasDriverLicence = false)
return "No";
else
return "";
}