为每个实例变量提供一个 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 语句。但我猜这不是“将其设置为空”。
我的妻子遇到了类似的问题,她将其设置在“获取”下,但我的工作方式不同。