我正在编写一个程序,为员工提供用户输入。我有一些私有setter(根据UML,它们必须是私有的),但方法名称下划线为灰色。我尝试过编写getters或做this.variableName来摆脱它们,但老实说,我没有太多使用私有setter的经验,所以我不确定该怎么做。另外,有些变量没有getter,也没有默认的构造函数,如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发布于 2020-11-01 13:24:16
你可以使用它们的一个地方是你的构造函数:
public Employee_Kubik(String n, double sala, int y, double sale){
name = n;
setSalary(sala);
setSales(sale);
setYearsWith(y);
}https://stackoverflow.com/questions/64628890
复制相似问题