当前位置:   article > 正文

java继承学习-员工增加工资_(二)编写java程序,定义employee 类以及 hr类,实现hr为employee加薪的功能。

(二)编写java程序,定义employee 类以及 hr类,实现hr为employee加薪的功能。实

首先员工和经理一样,都是员工,但是员工自有自己的基础工资,经理可能还会有一些额外的奖金,所以用继承来实现一个简单的代码。

员工类:

package com.example.extend;

import java.time.LocalDate;

public class Employee {

    private String name;

    private double salary;

    private LocalDate hireDay;

    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        hireDay = LocalDate.of(year, month, day);
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    /**
     * 员工增加工资
     * @param byPercent
     */
    public void raiseSalary(double byPercent) {
        double raise = salary*byPercent/100;
        salary += raise;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

经理类:

package com.example.extend;

public class Manager extends Employee {

    private double bonus;

    public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        bonus = 0;
    }

    @Override
    public double getSalary() {
        double baseSalary = super.getSalary();
        return baseSalary + bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

测试类:

   package com.example.extend;

public class ManagerTest {

    public static void main(String[] args) {
        // 构建一个管理员对象
        Manager boss = new Manager("Renyuhua", 80000, 2018, 12, 30);
        boss.setBonus(5000);

        Employee[] staff = new Employee[2];

        staff[0] = boss;
        staff[1] = new Employee("zhangzhouzhou", 20000, 2000, 6, 14);

        for (Employee employee : staff) {
            employee.raiseSalary(5);
            System.out.println("name = " + employee.getName() + ",salary = " + employee.getSalary());
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

最后的输出:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/486620
推荐阅读
相关标签
  

闽ICP备14008679号