Java 未使用局部变量的值
我正在学习java,在课堂上我们开始使用多个类。。。。 我有一个带有默认构造函数的employee类。在它里面我有默认值的变量。我遇到的问题是,默认构造函数中的变量值没有被使用,所以我在变量下面有黄色下划线。有人能把我引向正确的方向吗Java 未使用局部变量的值,java,Java,我正在学习java,在课堂上我们开始使用多个类。。。。 我有一个带有默认构造函数的employee类。在它里面我有默认值的变量。我遇到的问题是,默认构造函数中的变量值没有被使用,所以我在变量下面有黄色下划线。有人能把我引向正确的方向吗 package week2; import java.text.NumberFormat; public class Employee { //Instance Variables private String firstName = "",
package week2;
import java.text.NumberFormat;
public class Employee {
//Instance Variables
private String firstName = "", lastName = "";
private char gender = 'U';
private int dependents = 0;
private double annualSalary = 0;
//Default Employee Constructor
public Employee() {
String firstName = "not given";
String lastName = "not given";
char gender = 'U';
int dependents = 0;
double annualSalary = 20000;
} //End of default Employee Constructor
//Employee Constructor with variables
public Employee(String firstName, String lastName, char gender, int dependents, double salary) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.dependents = dependents;
this.annualSalary = salary;
} //End of Overloaded Employee Constructor
//Calculate Pay
public double annualSalary() {
return (getAnnualSalary() / 52);
} //End Calculate Pay
//Display Employee
public void displayEmployee() {
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println("First Name: \t" + this.firstName);
System.out.println("Last Name: \t" + this.lastName);
System.out.println("Gender: \t" + this.gender);
System.out.println("Dependents: \t" + this.dependents);
System.out.println("Annual Salary: \t" + nf.format(this.annualSalary));
System.out.println("Weekly Pay: \t" + nf.format(annualSalary()));
} //End Display Employee
//Getters and Setters
//Get-Set firstName
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//Get-Set lastName
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//Get-Set Gender
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
//Get-Set dependents
public int getDependents() {
return dependents;
}
public void setDependents(int dependents) {
this.dependents = dependents;
}
//Get-Set annualSalary
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
}
正如编译器所说,no-arg构造函数声明了新变量,并初始化了它们,但没有使用它们。请注意,这些变量不是类中的字段
public Employee() {
//this is a local variable in the method
String firstName = "not given";
//this variable is not the same as this:
this.firstName = "not given";
//same for other variables
}
对于这种情况,有两种解决方案:
public Employee() {
this.firstName = "not given";
//...
}
this()
并使用字段的初始值调用另一个构造函数:
public Employee() {
this("not given", "not given", 'U', 0, 2000);
}
正如编译器所说,no-arg构造函数声明了新变量,并初始化了它们,但没有使用它们。请注意,这些变量不是类中的字段
public Employee() {
//this is a local variable in the method
String firstName = "not given";
//this variable is not the same as this:
this.firstName = "not given";
//same for other variables
}
对于这种情况,有两种解决方案:
public Employee() {
this.firstName = "not given";
//...
}
this()
并使用字段的初始值调用另一个构造函数:
public Employee() {
this("not given", "not given", 'U', 0, 2000);
}
您正在默认构造函数中创建
局部变量。删除该声明,它将为类级变量赋值
//Default Employee Constructor
public Employee() {
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
} //End of default Employee Constructor
注意:黄色表示警告。在您的例子中,它暗示您正在创建局部变量并为其赋值,这在构造函数中不使用,并且局部变量在构造函数外部不可见
如果没有使用相同名称定义其他局部变量,则可以使用this.firstName
或firstName
。(在您的案例中,您可以在默认构造函数中使用这两个选项)
如果某个局部变量是用相同的名称定义的,则firstName
指的是局部变量,而指的是类级变量。(可以在参数化构造函数中看到)您正在默认构造函数中创建局部变量。删除该声明,它将为类级变量赋值
//Default Employee Constructor
public Employee() {
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
} //End of default Employee Constructor
注意:黄色表示警告。在您的例子中,它暗示您正在创建局部变量并为其赋值,这在构造函数中不使用,并且局部变量在构造函数外部不可见
如果没有使用相同名称定义其他局部变量,则可以使用this.firstName
或firstName
。(在您的案例中,您可以在默认构造函数中使用这两个选项)
如果某个局部变量是用相同的名称定义的,那么firstName
指的是局部变量,而指的是类级变量。(你可以在参数化构造函数中看到)这里你必须清楚的概念是变量的范围,
在默认构造函数中
//Default Employee Constructor
public Employee() {
String firstName = "not given";
String lastName = "not given";
char gender = 'U';
int dependents = 0;
double annualSalary = 20000;
} //End of default Employee Constructor
声明的变量仅在此构造函数中具有作用域,它们不同于类开头声明的实例变量。
因此,这些变量不会在任何地方使用,因此编译器警告。这里您必须明确的概念是变量的范围,
在默认构造函数中
//Default Employee Constructor
public Employee() {
String firstName = "not given";
String lastName = "not given";
char gender = 'U';
int dependents = 0;
double annualSalary = 20000;
} //End of default Employee Constructor
声明的变量仅在此构造函数中具有作用域,它们不同于类开头声明的实例变量。
因此,这些变量不会在任何地方使用,因此编译器会发出警告。您正在默认构造函数中创建局部变量。只需像在参数化构造函数中那样设置值,或者在构造函数中创建局部变量。创建Employee
的新实例后,这些变量将不存在。考虑在默认构造函数中设置私有字段(将String firstname=
替换为this.firstname=
。您正在默认构造函数中创建局部变量。您只需像在参数化构造函数中那样设置值。您正在构造函数中创建局部变量。创建的新实例后,这些变量将不存在。)>雇员< /代码>。考虑在默认构造函数中设置私有字段(替换<代码>字符串FiSTNEST= > <代码>此.FieldNe= 。谢谢所有人的快速响应。LuigGi方法是我选择的方法。我只希望教员接受它。(对于这种情况,哪一个是最好的),然后请邀请他访问该站点并了解真正的Java程序员是如何工作的:)@Monti,如果这回答了你的问题,那么你应该接受它。你只能接受一个答案。谢谢大家的快速回答。Luiggi你的方法就是我选择的方法。我只希望教练接受它。Lol如果你的教练不接受第二个(这对这个案例来说是最好的),然后请邀请他访问该网站,了解真正的Java程序员是如何工作的:)@Monti,如果这回答了你的问题,那么你应该接受它。你只能回答一个问题。