如何制定使员工年满一岁的方法?

时间:2018-02-08 13:03:51

标签: java

我刚开始学习在我的教育中使用Java编程。这意味着,我还是Java的新手。

我有一些代码,我应该制作一个名为birthday()的方法,让员工年满一岁,然后展示员工的新年龄。

我有一个名为Employee.java的类和一个名为EmployeeApp.java的类。

Employee.java代码如下所示:

package model;

public class Employee {

    private String name;
    private boolean trainee;
    private int age;


    public Employee(String inputName, int inputAge) {
        name = inputName;
        age = inputAge;
        trainee = true;
    }


    public void setName(String inputName) {
        name = inputName;
    }

    public void setAge(int inputAge) {
        age = inputAge;
    }


    public String getName() {
        return name;
    }


    public int getAge() {
        return age;
    }

    public int birthday() {
        agePlusOne = age + 1;
    }

    public void setTrainee(boolean isTrainee) {
        trainee = isTrainee;
    }


    public boolean isTrainee() {
        return trainee;
    }

    public void printEmployee() {
        System.out.println("*******************");
        System.out.println("Name: " + name);
        System.out.println("Trainee: " + trainee);
        System.out.println("Age: " + age);
        System.out.println();
    }
}

EmployeeApp.java代码如下所示:

 package model;

 public class EmployeeApp {

   public static void main(String[] args) {
        Employee e1 = new Employee("Hans Jensen", 23);
        e1.printEmployee();
        Employee e2 = new Employee("Rasmus Nielsen", 32);
        e2.printEmployee();

   }

}

任何帮助将不胜感激! : - )

5 个答案:

答案 0 :(得分:1)

public int birthday() {

        return age++;
}

此后在printEmployee()方法中添加一行代码System.out.println("Age+1: is"+birthday());

完整代码:

public void printEmployee() {
        System.out.println("*******************");
        System.out.println("Name: " + name);
        System.out.println("Trainee: " + trainee);
        System.out.println("Age: " + age);

        System.out.println("Age+1: is"+birthday());//add this line
        System.out.println();
    }

答案 1 :(得分:1)

首先你应该问问自己,你真正被问到了什么。我理解它的方式,birthday方法应该使员工年龄提前一年,因此它应该更改其age变量的值。

您的birthday函数将age + 1存储在新变量agePlusOne中(未声明且确实应该存在),因此它会进行计算,但不会对员工产生影响#39; s age。您需要将age + 1的结果存储在age中,或者使用现有函数setAge为您执行此操作(将计算作为参数传递)。

接下来,您需要显示员工的新年龄。我不清楚你应该在哪里这样做。有意义的是拥有一个birthday函数,该函数在调用时更改年龄,并且不会输出到屏幕(除了可能暂时用于调试目的)。您可以在main函数中显示其效果,例如在现有函数之后添加两行代码:

// existing lines
Employee e2 = new Employee("Rasmus Nielsen", 32);
e2.printEmployee(); // shows age before calling birthday
// new lines
e2.birthday();      // changes value of age
e2.printEmployee(); // shows age after calling birthday

如果您认为必须在birthday函数中执行输出,只需复制printEmployee中显示年龄的行。

答案 2 :(得分:0)

func slideOutMenus() {
  if revealViewController() != nil {
      hamburgMenuBtn.target = revealViewController()
  hamburgMenuBtn.action = #selector(SWRevealViewController.revealToggle(_:))
  revealViewController().rearViewRevealWidth = 250
  view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
  }
 }

但是你不应该这样做,因为打印应该在public int ageUp() { System.out.println(++age); return age; } 方法之外。

答案 3 :(得分:0)

这听起来有点模糊,但您可以将实施更改为age字段,而不是使用birthday字段...

public class Employee {

    private String name;
    private boolean trainee;
    private Date birthday; //changes takes place in here
    ...

}

每当您询问员工年龄时,您都会提到某个特定日期

public int getAgeInYears(Date referenceDate){
    int age = ... // some Math to calculate age - please do on your own
    return age;
}

在行动中看起来像

int ageOnEntrance = myEmployee.getAgeInYears(new Date(1997));
int ageNow = myEmployee.getAgeInYears(new Date(now())); 
int ageOnRetirement = myEmployee.getAgeInYears(new Date(2018));  //lucky guy
int candlesOnCakeIn2002 = myEmployee.getAgeInYears(new Date(2002)); 
int candlesOnCakeLastYear = myEmployee.getAgeInYears(new Date(2017)); 

答案 4 :(得分:-1)

更改方法birthday()

public int birthday() {
    return ++age; //return new age
}