私人 Setter 说 setter 方法是 "not used"
Private Setter saying setter method is "not used"
我正在编写一个程序,为员工收集用户输入。我有一些私有 setter(根据 UML,它们必须是私有的)但是方法名称带有灰色下划线。我试过制作 getter 或做 this.variableName 来摆脱它们,但老实说,我对私有 setter 没有太多经验,所以我不确定该怎么做。此外,某些变量没有吸气剂,也没有默认构造函数,如 UML 中所示。
UML:
这是我的代码
public class Employee_Kubik {
//variables
private static String name;
private double salary;
private int yearsWith;
private double sales;
public Employee_Kubik(String n, double sala, int y, double sale){
name = n;
salary = sala;
yearsWith = y;
sales = sale;
} //Employee end
public String getName(){
return name;
} //getName end
private void setSalary(double s){
if (s > 0){
salary = s;
} //if end
else{
salary = 0;
} //else end
} //setSalary end
private void setYearsWith(int yw){
if (yw > 0){
yearsWith = yw;
} //if end
else{
yearsWith = 0;
} //else end
} //setYearsWith
private void setSales(double s){
if(s > 0){
sales = s;
} //if end
else{
sales = 0;
} //else end
} //setSales end
public boolean promote(){
if(sales > 9999 && yearsWith > 2){
return true;
} //if end
else{
return false;
} //else end
} //promote end
public double calculateRaise(){
salary = salary * 0.05;
return salary;
} //calculateRaise end
@Override
public String toString(){
return "Employee Name: " +
name +
", has been with the company for " +
yearsWith +
" years and last year sold a total of $" +
sales +
"\nPromotion Status = " +
promote();
} //toString end
} //class end
您可以在其中使用它们的一个地方是您的构造函数:
public Employee_Kubik(String n, double sala, int y, double sale){
name = n;
setSalary(sala);
setSales(sale);
setYearsWith(y);
}
我正在编写一个程序,为员工收集用户输入。我有一些私有 setter(根据 UML,它们必须是私有的)但是方法名称带有灰色下划线。我试过制作 getter 或做 this.variableName 来摆脱它们,但老实说,我对私有 setter 没有太多经验,所以我不确定该怎么做。此外,某些变量没有吸气剂,也没有默认构造函数,如 UML 中所示。
UML:
这是我的代码
public class Employee_Kubik {
//variables
private static String name;
private double salary;
private int yearsWith;
private double sales;
public Employee_Kubik(String n, double sala, int y, double sale){
name = n;
salary = sala;
yearsWith = y;
sales = sale;
} //Employee end
public String getName(){
return name;
} //getName end
private void setSalary(double s){
if (s > 0){
salary = s;
} //if end
else{
salary = 0;
} //else end
} //setSalary end
private void setYearsWith(int yw){
if (yw > 0){
yearsWith = yw;
} //if end
else{
yearsWith = 0;
} //else end
} //setYearsWith
private void setSales(double s){
if(s > 0){
sales = s;
} //if end
else{
sales = 0;
} //else end
} //setSales end
public boolean promote(){
if(sales > 9999 && yearsWith > 2){
return true;
} //if end
else{
return false;
} //else end
} //promote end
public double calculateRaise(){
salary = salary * 0.05;
return salary;
} //calculateRaise end
@Override
public String toString(){
return "Employee Name: " +
name +
", has been with the company for " +
yearsWith +
" years and last year sold a total of $" +
sales +
"\nPromotion Status = " +
promote();
} //toString end
} //class end
您可以在其中使用它们的一个地方是您的构造函数:
public Employee_Kubik(String n, double sala, int y, double sale){
name = n;
setSalary(sala);
setSales(sale);
setYearsWith(y);
}