【问题标题】:JAVA Get & Set methods with calculations带有计算的 JAVA Get 和 Set 方法
【发布时间】:2018-02-09 13:08:45
【问题描述】:

Java 新手并尝试设置 get 和 set 方法。尽管我需要根据 getSalary() 乘以 getMonths() 来计算年初至今的总数。

我无法确定是否可以在 get 或 set 方法中进行计算,或者我可能只是一个新手,我把它放在错误的地方。

public class Employee_v2
{
    //Instance variables
    private String first_name;
    private String last_name;
    private double salary;
    private int months;
    final double increase= 0.25;
    private double year_to_date;

    public Employee_v2()
    {
    //fill in the code to set default values to the instance variables
        first_name="";
        last_name="";
        salary= 0.0;
        months= 0; 
    }

    //Constructor initializing instance variables with arguments
    public Employee_v2(String f, String l, Double sal, int mo)
    {
        first_name = f;
        last_name = l;
        salary = sal;
        months = mo;  
    }

//set arguments     
    public void setFirst(String f)
    {
        first_name = f;
    }   
    public void setLast (String l)
    {
        last_name = l;
    }   
    public void setSalary (double sal)
    {
        salary = sal;
    }  
    public void setMonths (int mo)
    {
        months = mo;
    } 
    public void setYtdSalary ()
    {
        double yearToDateSal;
        yearToDateSal = getSalary() * getMonths();
    }


//get arguments 
    public String getFirst()
    {
        return first_name;
    }

   public String getLast()
    {
        return last_name;
    }

   public double getSalary()
    {
        return salary;
    }

   public int getMonths()
   {
        return months;
   }

   public double getYtdSalary()
   {
        return yearToDateSal;     
   }


//DISPLAY

   public void displayEmployee()
   {
       //display the name and salary of both Employee objects
       System.out.println("Employee first name: "  + getFirst());
       System.out.println("Employee last name: " + getLast());
       System.out.printf("Employee salary: $%.2f\n", getSalary());

       //complete the code
       System.out.println("-------------------------------");

       //Determine the year to date salary for both employee
       System.out.printf(getFirst() + "'s salary:  $%.2f\n", getSalary());

       // System.out.println(jas.getSalary());
       System.out.printf("Year to date salary: $%.2f\n", getYtdSalary());

       //set and display salary with increase
       setSalary(getSalary()+ getSalary()*increase);
       System.out.printf("New Salary: $%.2f\n", getSalary());

       System.out.println();
       System.out.println();
   }//end method
}//end Class

【问题讨论】:

  • 有错误吗?
  • 如果它可以编译就意味着你可以。公开集合方法和一般的可变性是不受欢迎的。除了在 set 方法中设置值之外,还可以做任何事情。我看不出有一个公共的“setYtdSalary()”方法的意义,在getter中这样做......
  • 约定是通过驼峰式命名您的 getter 和 setter 实例变量。例如 setFirst 是错误的(实例变量应该是“first”而不是 first_name)。您的 IDE 可以为您的 getter 和 setter 生成代码
  • Stack Overflow 是针对非常具体的狭隘问题的。请说明您的确切问题。
  • @BasilBourque 似乎足够具体,可以很快解决问题。感谢您的意见。

标签: java methods get set


【解决方案1】:

在 getter 和 setter 中可以进行计算。我会做这样的事情:

public double getYtdSalary(){
   double ytd = 0;
   ytd = months * salary;
   return ytd;     
}

这样,您可以即时计算年初至今的数据,并且始终是准确的数据。例如,如果您调用了 setMonths 方法,您还必须在调用 getYtdSalary 之前调用 setYtdSalary。

此外,无需在您的类中调用您的 getter 来访问您的私有变量。

我还建议在您的类中使用 Javadoc 注释,因为它们更容易理解方法的作用。

【讨论】:

  • 我还会将您的实例变量命名为“first”而不是“first_name”,我通常在我的 setter 中设置我的变量,如下所示 this.first = f;
  • 一个问题,我试图解释你的意思是“另外,没有必要在你的类中调用你的 getter 来访问你的私有变量。”
  • Private 意味着这些变量只能在此类中访问,因此您可以在定义它的类中访问它,而无需调用您的 getter。使用您的 getter 获取它们仍然是一种好习惯,特别是如果您在返回数据之前对其进行操作。
【解决方案2】:

回答您的问题:是的,可以进行计算 这在很多地方也是需要的,因为使用 get 和 set 方法的主要原因是您可以在将值设置到类中之前检查或操作这些值

我猜你的代码在打印年初至今的工资时会崩溃,因为变量 yearToDateSal 仅在你设置的方法的范围内有效。

public void setYtdSalary ()
{
    double yearToDateSal;
    yearToDateSal = getSalary() * getMonths();
}

你应该在类的开头声明你的 YearToDateSal 变量,即紧随其后

final double increase= 0.25;
private double year_to_date;
private double yearToDateSal;

并在您的 setYtdSalary() 中删除 yearToDateSal 的声明;

这样你应该没问题:)

【讨论】:

  • 说得通,我才弄了几个星期。感谢您的知识
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多