首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Private Setter声明setter方法“未使用”

Private Setter声明setter方法“未使用”
EN

Stack Overflow用户
提问于 2020-11-01 13:15:57
回答 1查看 35关注 0票数 1

我正在编写一个程序,为员工提供用户输入。我有一些私有setter(根据UML,它们必须是私有的),但方法名称下划线为灰色。我尝试过编写getters或做this.variableName来摆脱它们,但老实说,我没有太多使用私有setter的经验,所以我不确定该怎么做。另外,有些变量没有getter,也没有默认的构造函数,如UML所示。

UML:

下面是我的代码

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-01 13:24:16

你可以使用它们的一个地方是你的构造函数:

代码语言:javascript
复制
public Employee_Kubik(String n, double sala, int y, double sale){
    name = n;
    setSalary(sala);
    setSales(sale);
    setYearsWith(y);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64628890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档