Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaget&;用计算设置方法_Java_Methods_Get_Set - Fatal编程技术网

javaget&;用计算设置方法

javaget&;用计算设置方法,java,methods,get,set,Java,Methods,Get,Set,Java新手,正在尝试设置get和set方法。虽然我需要做一个基于getSalary()乘以getMonths()的计算,以得到一年迄今为止的总数 我还没能弄清楚get或set方法是否可以计算,或者我可能只是个新手,我把它放错地方了 public class Employee_v2 { //Instance variables private String first_name; private String last_name; private double s

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

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

public double getYtdSalary(){
   double ytd = 0;
   ytd = months * salary;
   return ytd;     
}
这样,您可以动态计算到目前为止的年份,并且始终是准确的数据。例如,如果调用setMonths方法,那么在调用getYtdSalary之前还必须调用setYtdSalary

另外,不需要在类中调用getter来访问私有变量


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

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

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

public void setYtdSalary ()
{
    double yearToDateSal;
    yearToDateSal = getSalary() * getMonths();
}
您应该在类的开头声明YearToDateSal变量,即rigth after

final double increase= 0.25;
private double year_to_date;
private double yearToDateSal;
在你的setYtdSalary()中删除yearToDateSal的声明


这样你就可以了:)

有错误吗?如果它编译了,就意味着你可以。公开集合方法和通常的易变性是不一致的。除了在set方法中设置一个值之外,还可以执行其他操作。我不认为有一个公共的“setYtdSalary()”方法有什么意义,在getter中这样做……约定是通过对实例变量进行骆驼式封装来命名getter和setter。e、 g setFirst错误(实例变量应为“first”而不是first_name)。IDE可以为Getter和settersStack生成代码。Stack Overflow用于非常特定的狭义问题。请说明您的确切问题。@BasilBourque似乎足够具体,可以相当快地解决问题。不过,谢谢你的意见。说得通,我才搞了几个星期。感谢您的知识,我也会将您的实例变量命名为“first”,而不是“first_name”,我通常在setter中设置变量,如下所示。first=f;有一个问题,我试图解释你所说的“也没有必要在类中调用getter来访问私有变量”是什么意思。private意味着这些变量只能在这个类中访问,因此你可以在定义它的类中访问它,而无需调用getter。使用getter获取数据仍然是一种很好的做法,尤其是在返回数据之前处理数据时。