JAVA面向对象练习题,课后编程题。题目为:公司员工分为5类,每类员工都有相应的封装类。

abstract class Employee{
    private int month;
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }
    public double  getSalary(int month){
        int salary = 0;
        if(month==this.month)
            salary=salary+100;
        return salary;
    }
    public Employee(){

    }
    public Employee(String name,int month){
        this.month=month;
        this.name=name;
    }
}
class SalariedEmployee extends Employee{
   private int monthSalary;
   public SalariedEmployee(){

   }
   public SalariedEmployee(String name,int month,int monthSalary){
       super(name,month);
       this.monthSalary=monthSalary;
   }
   public int getMonthSalary(){
       return monthSalary;
   }
   public void setMonthSalary(int monthSalary){
       this.monthSalary = monthSalary;
   }
   public double getSalary(int month){
      double salary = monthSalary+super.getSalary(month);
       return salary;
   }
}
class HourlyEmployee extends Employee{
    public double getHourlySalary() {
        return hourlySalary;
    }

    public void setHourlySalary(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    private double hourlySalary;
    private int hours;
    public HourlyEmployee(){

    }
    public HourlyEmployee(String name,int month,double hourlySalary,int hours){
        super(name,month);
        this.hourlySalary=hourlySalary;
        this.hours=hours;
    }
    public double getSalary(int month){
        if(hours<0){
            System.out.println("数据错误!!!!!");
            return 0;
        }
        else if(hours<=160)
            return hourlySalary*hours+super.getSalary(month);
        else return hourlySalary*160+hourlySalary*1.5*(hours-160)+super.getSalary(month);
    }
}
class SalesEmployee extends Employee{
    private double sales;
    private double rete;
    public double getSales() {
        return sales;
    }

    public void setSales(double sales) {
        this.sales = sales;
    }

    public double getRete() {
        return rete;
    }

    public void setRete(double rete) {
        this.rete = rete;
    }

    public SalesEmployee(){

    }
    public SalesEmployee(String name,int month,double sales,double rete){
        super(name,month);
        this.sales=sales;
        this.rete=rete;
    }

    @Override
    public double getSalary(int month) {
        return this.getSales()*(1+this.getRete())+super.getSalary(month);
    }
}
class BasePlusSalesEployee extends SalesEmployee{
    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    private double baseSalary;
    public BasePlusSalesEployee(){

    }
    public BasePlusSalesEployee(String name,int month,double sales,double rete,double baseSalary){
        super(name,month,sales,rete);
        this.baseSalary=baseSalary;
    }
    public double getSalary(int month){
        return baseSalary+super.getSalary(month);
    }
}
public class 一 {
    public static void main(String[] args) {
        Employee[] employees = {new SalariedEmployee("张三",1,1600),new HourlyEmployee("李 四",2,50,180),new SalesEmployee("王五",3,6500,0.15),
                new BasePlusSalesEployee("赵六",4,5000,0.15,2000)};
        for(int i=0;i<employees.length;i++){
            System.out.println(Math.round(employees[i].getSalary(5)));
        }
    }
}

某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下
(1)Employee:这是所有员工的父类。
①属性:员工的姓名、员工的生日月份。
)方法:getSalary(int month)根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。
(2)SalariedEmployee:Employee 的子类,拿固定工资的员工。
属性:月薪。
(3)HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的
部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数。
(4)SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率。
(5)BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为
底薪加上销售提成。
属性:底薪。
本题要求根据上述员工分类,编写一个程序,实现以下功能:
(1)创建一个Employee数组,分别创建若干不同的Employee对象,并打印某个月的
(2)每个类都完全封装,不允许有非私有化属性。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年12月4日
下一篇 2023年12月4日

相关推荐