每当我在客户端程序中使用Mutator方法时,我只需要澄清一下好吧所以我的员工类设置好,我被告知我的变量应该是private double salary
然后让我的Accessors然后是Mutators,它应该是public void setSalary (double newSalary)
,里面有salary = newSalary
现在,我在eclipse中对此进行编码,而且我在我的客户端程序中,所以我把Employee.setSalary(200.00);
我所教的是正确的方式,我们在课堂上使用的书也是这样的.. 。
然而,Eclipse告诉我,我的mutator应该是public static void setSalary (double newSalary);
并且我的初始变量应该是private static double salary
。为什么说这本书和我的教授说的是另一种方式呢?
所以这是我班级的代码
import java.text.DecimalFormat;
public class Employee {
private String Last_Name;
private String First_Name;
private int Years_in_Company;
private double Salary;
private char Status;
private String Section;
public Employee(){
Last_Name = "unknown";
First_Name = "unkown";
Years_in_Company = 0;
Salary = 0.0;
Status = '\u0000';
Section = "unkown";
}
public String getLast_Name(){
return Last_Name;
}
public String getFirst_Name(){
return First_Name;
}
public int getYears_in_Company(){
return Years_in_Company;
}
public double getSalary(){
return Salary;
}
public char getStatus (){
return Status;
}
public String getSection(){
return Section;
}
public void setLast_Name(String newLast_Name) {
Last_Name = newLast_Name;
}
public void setFirst_Name(String newFirst_Name){
First_Name = newFirst_Name;
}
public void setYears_in_Company(int newYears_in_Company){
if( newYears_in_Company >= 0)
Years_in_Company = newYears_in_Company;
else{
System.err.println ("Years in company cannot be negative");
System.err.println ("Value not changed");
}
}
public void setSalary (double newSalary){
if ( newSalary >= 2000.00){ //good practice to add braces to `if`condition.
Salary = newSalary;
}
else{
System.err.println("Salary cannot be less than $2,000.00");
System.err.println("Value not changed");
}
}
public void setStatus(char newStatus){
if (newStatus == 'a' || newStatus == 'r'){
Status = newStatus;
}
else{
System.err.println ("Status cannot be anything other than a or r");
System.err.println ("Value not changed");
}
}
public void setSection (String newSection){
Section = newSection;
}
public void display (){
System.out.println ( Last_Name + First_Name + Years_in_Company + Salary + Status + Section);
}
public String toString (){
DecimalFormat SalaryFormat = new DecimalFormat ("#0.0");
return "Name: " + Last_Name
+ ", " + First_Name
+ ", Years in Company: " + Years_in_Company
+ ", Salary: " + SalaryFormat.format ( Salary )
+ ", Status: " + Status
+ ", Section: " + Section;
}
public boolean equals( Object o){
if (!(o instanceof Employee) ){
return false;
}
else{
Employee objEmployee = (Employee) o;
if ( Last_Name.equals (objEmployee.Last_Name) &&
First_Name.equals (objEmployee.First_Name) &&
Math.abs(Years_in_Company - objEmployee.Years_in_Company) < 0.0001 &&
Math.abs(Salary - objEmployee.Salary) < 0.0001 &&
Status == objEmployee.Status &&
Section.equals(objEmployee.Section))
return true;
else
return false;
}
}
}
我只是想知道为什么Eclipse告诉我我的mutator应该是public static void setSalary (double newSalary);
并且我的初始变量应该是private static double salary
。为什么说这本书和我的教授说的是另一种方式呢?