0

为每个实例变量提供一个 set 和一个 get 方法。如果月薪不是正数,不要设置它的值。

我遇到的问题是“未设置值”这是我的代码:

主类

public class Main {

public static void main(String[] args) {

Employee myEmp  = new Employee("William", "Russell", 30000);
Employee myEmp2  = new Employee("Yevgeniya", "Pakhom", -1);
myEmp.displayMessage();
myEmp2.displayMessage();
}
}

Employee.class


public class Employee {

    private String fName;
    private String lName;
    private double ySalary;

public Employee (String firstName, String lastName, double yearSalary)
    {
        fName = firstName;
        lName = lastName;
        ySalary = yearSalary;
    }
// Set First Name
    public void setFName (String firstName)
        {
        fName = firstName;
        }
// Get First Name
    public String getFName()
        {
        return fName;
        } 

   // Set Last Name
    public void setLName (String lastName)
        {
        lName = lastName;
        }
  //  Get Last Name
    public  String getLName()
        {
        return lName;
        } 

 // Set Salary
    public void setSalary (double yearSalary)
        {

            ySalary = yearSalary;

        }
// Get Salary
    public double getYearSalary()
        {

        return ySalary;

        }
// Display method
        public void displayMessage()
        {
            if (ySalary < 0)
                 System.out.printf("Hello %s %s, \nI see you're       currently making: %.2f\n", getFName(), getLName(),getYearSalary() );


 }
}

我在 displayMessage() 方法中添加了 if 语句。但我猜这不是“将其设置为空”。

我的妻子遇到了类似的问题,她将其设置在“获取”下,但我的工作方式不同。

4

8 回答 8

1

将 setSalary 更改为

public void setSalary (double yearSalary)
        {
            if (yearSalary <= 0) {
                 throw new IllegalArgumentException("yearSalary: " + yearSalary);
            }
            ySalary = yearSalary;
        }
于 2013-03-05T07:08:19.240 回答
0

你不应该在你的setSalary()方法中设置它。那是你settersalary

在你的情况下,里面Constructor就足够了。

public Employee (String firstName, String lastName, double yearSalary)
{
        fName = firstName;
        lName = lastName;
        if(yearSalary >= 0){
            ySalary = yearSalary;
        }
}
于 2013-03-05T07:05:46.947 回答
0

您需要在构造函数中调用此函数。或者只是if在构造函数中放置一个。

于 2013-03-05T07:06:51.613 回答
0

您应该检查 setSalary 方法中的先决条件,并且可以选择抛出异常(如 IllegalArgumentException)。

于 2013-03-05T07:07:44.277 回答
0
class Employee{

   //Use factory method instead to check for pre conditions
   public static Employee newEmployee(String firstName, String lastName, double salary){
       if(salary < 0){
          throw new IllegalArgumentException("Salary cannot be negetive");
       }

      return new Employee(firstName, lastName, salary);
   }

}

如果您还有一个 setter 方法,那么还应该在那里检查否定的条件以抛出非法参数异常。

为什么使用工厂方法:由于在构造函数中工作不是一个好习惯,所以在工厂方法或实际工厂中进行前置条件检查是最好的做法。

于 2013-03-05T07:10:07.217 回答
0

在 setSalary() 中,您可以检查薪水是否<0,如果是则不设置

public void setSalary (double yearSalary)
        {
            if(yearSalary >0 ){
                 ySalary = yearSalary;
           }
        }
于 2013-03-05T07:11:07.730 回答
0

您正在通过和构造函数设置薪水,并且根本不使用 setter。如果需要,在构造函数中添加验证。

于 2013-03-05T07:12:22.780 回答
0

我修改了代码以使用 Double。希望这可以帮助您设置空值。

public class JavaMainTest {

public static void main(String[] args) {

Employee myEmp  = new Employee("William", "Russell", 30000d);
Employee myEmp2  = new Employee("Yevgeniya", "Pakhom", null);
Employee myEmp3  = new Employee("Sernea", "Monsen", -1d);
myEmp.displayMessage();
myEmp2.displayMessage();
myEmp3.displayMessage();
}
}



 class Employee {

    private String fName;
    private String lName;
    private Double ySalary;

public Employee (String firstName, String lastName, Double yearSalary)
    {
        fName = firstName;
        lName = lastName;
        if (yearSalary!=null)
        ySalary = yearSalary;
    }
// Set First Name
    public void setFName (String firstName)
        {
        fName = firstName;
        }
// Get First Name
    public String getFName()
        {
        return fName;
        } 

   // Set Last Name
    public void setLName (String lastName)
        {
        lName = lastName;
        }
  //  Get Last Name
    public  String getLName()
        {
        return lName;
        } 

 // Set Salary
    public void setSalary (Double yearSalary)
        {

            ySalary = yearSalary;

        }
// Get Salary
    public Double getYearSalary()
        {

        return ySalary;

        }
// Display method
        public void displayMessage()
        {
            if (ySalary == null)
                 System.out.printf("Hello %s %s, \nI see you're       currently making: %.6f\n", getFName(), getLName(),getYearSalary() );
            else if (ySalary < 0)
                 System.out.printf("Hello %s %s, \nI see you're       currently making: %.2f\n", getFName(), getLName(),getYearSalary() );


 }
}
于 2013-03-05T07:14:32.750 回答