我的日食一直在说 "The value of the field is not used"
My eclipse keep saying "The value of the field is not used"
我的 Person class 中有一个 (boolean)hasDriverLicence 变量。我创建了 getter 和 setter 方法,我在 person 构造函数中使用了 hasDriverLicence 但我的日食说 "The value of the field Person.hasDriverLicence is not used." 这是代码:
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 "";
}
您在 getter 中有错字。
您的 if
条件实际上设置了实例字段的值,而不是检查它:
if (this.hasDriverLicence = true)
这应该是:
if (this.hasDriverLicence == true)
或者更简单:
if (this.hasDriverLicence) {
// ...
// no need for a separate if statement for the opposite condition,
// and you can only have two states here
else {
// ...
}
变量因此被赋值,但从未在您的代码中使用。
详解
为什么单个 =
编译,但 IDE 给你一个警告,声称变量从未被使用,是因为 赋值运算符 returns分配的值。
例如语句:
myVariable = 1
...returns1
。
因此,当您错误地检查赋值 (=
) 而不是原始相等性 (==
) 时,您将始终检查 value 你的任务(在你的情况下,true
在第一个条件中总是满足,在第二个条件中 false
,因此永远不会满足)。
或许您可以尝试重建您的工作区。我看不到上述代码的问题。
我的 Person class 中有一个 (boolean)hasDriverLicence 变量。我创建了 getter 和 setter 方法,我在 person 构造函数中使用了 hasDriverLicence 但我的日食说 "The value of the field Person.hasDriverLicence is not used." 这是代码:
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 "";
}
您在 getter 中有错字。
您的 if
条件实际上设置了实例字段的值,而不是检查它:
if (this.hasDriverLicence = true)
这应该是:
if (this.hasDriverLicence == true)
或者更简单:
if (this.hasDriverLicence) {
// ...
// no need for a separate if statement for the opposite condition,
// and you can only have two states here
else {
// ...
}
变量因此被赋值,但从未在您的代码中使用。
详解
为什么单个 =
编译,但 IDE 给你一个警告,声称变量从未被使用,是因为 赋值运算符 returns分配的值。
例如语句:
myVariable = 1
...returns1
。
因此,当您错误地检查赋值 (=
) 而不是原始相等性 (==
) 时,您将始终检查 value 你的任务(在你的情况下,true
在第一个条件中总是满足,在第二个条件中 false
,因此永远不会满足)。
或许您可以尝试重建您的工作区。我看不到上述代码的问题。