我正在学习java,在课堂上我们开始使用多个类.... 我有一个带有默认构造函数的employee类。在其中我有变量的默认值。我遇到的问题是没有使用默认构造函数中的变量值,所以我在变量下得到黄色下划线。有人可以引导我朝着正确的方向前进。
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;
}
}
答案 0 :(得分:3)
您正在默认构造函数中创建local variables
。删除声明,它将为类级变量赋值。
//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
引用局部变量,this.firstName
引用类级别变量。(您可以在参数化构造函数中看到)
答案 1 :(得分:2)
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);
}
答案 2 :(得分: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
声明的变量仅在此构造函数中具有范围,它们与在类的开头声明的实例变量不同。 因此,这些变量不会在任何地方使用,因此编译器警告。