Java manager and employee difference implementation approach

  • 2020-04-01 03:02:04
  • OfStack

There is a lot of commonality between the experience of working in the same company and the employee. For example, the salary is paid every month, but the manager will get a bonus after completing the target task. At this point, writing a manager class with an employee class would result in a lot less code, and using inheritance techniques would allow the manager class to use the properties and methods defined in the employee class. Write programs that demonstrate the differences between managers and employees by inheriting them.

Analysis of train of thought: typical inheritance problem. The parent class is the employee class, the subclass is the manager class, and the manager class inherits the employee class. In this way, only the additional bonus is implemented in the manager class, that is, the additional member variables to represent the bonus and the member methods to set and get the bonus are added.

The code is as follows:


import java.util.Date;      

public class Employee {

    private String name;        //Employee's name
    private double salary;      //Employee's salary
    private Date birthday;      //Employee's birthday

    public String getName() {       // To obtain Employee's name
        return name;
    }

    public void setName(String name) {  // Set up the Employee's name
        this.name = name;
    }

    public double getSalary() {     // To obtain Employee's salary
        return salary;
    }

    public void setSalary(double salary) {  // Set up the Employee's salary
        this.salary = salary;
    }

    public Date getBirthday() {     // To obtain Employee's birthday
        return birthday;
    }

    public void setBirthday(Date birthday) {        // Set up the Employee's birthday
        this.birthday = birthday;
    }

}
public class Manager extends Employee {
    private double bonus;//Manager's bonus

    public double getBonus() {//Get the manager's bonus
        return bonus;
    }

    public void setBonus(double bonus) {//Set the manager's bonus
        this.bonus = bonus;
    }
}
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        Employee employee = new Employee();//Create and assign a value to the Employee object
        employee.setName("Java");
        employee.setSalary(100);
        employee.setBirthday(new Date());
        Manager manager = new Manager();//Create and assign a value to the Manager object
        manager.setName(" Tomorrow's science and technology ");
        manager.setSalary(3000);
        manager.setBirthday(new Date());
        manager.setBonus(2000);
        //Output attribute values for managers and employees
        System.out.println("Employee's name : " + employee.getName());
        System.out.println("Employee's salary : " + employee.getSalary());
        System.out.println("Employee's birthday : " + employee.getBirthday());
        System.out.println(" Manager's name: " + manager.getName());
        System.out.println(" Salary of the manager: " + manager.getSalary());
        System.out.println(" Manager's birthday: " + manager.getBirthday());
        System.out.println(" Bonus for the manager: " + manager.getBonus());
    }
}

The effect is shown in the figure:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201431163609810.png" >


Related articles: