chp6-
最编程
2024-07-21 20:33:04
...
**(封装、继承、super)某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(intmonth) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
SalariedEmployee:Employee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
根 据 要 求 创 建SalariedEmployee、HourlyEmployees、SaleEmployee 和BasePlusSalesEmployee四个类的对象各一个,并计算某个月这四个对象的工资。注意:要求把每个类都做成完全封装,不允许非私有化属性。类图如下:
Employee.java
package chap6_24;
public class Employee {
private String name;//员工的姓名
private int month;//员工的生日月份
//构造方法
public Employee(String name, int month) {
super();
this.name = name;
this.month = month;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
//根据月份确定工资
public double getSlary(int month){
return this.month==month?100:0;
}
public int getMonth() {
return month;
}
}
SalariedEmployee.java
package chap6_24;
public class SalariedEmployee extends Employee{
private double salary;//月薪
//构造方法
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public SalariedEmployee() {
super();
// TODO Auto-generated constructor stub
}
public double getSalary(int month) {
return salary+super.getSlary(month);
}
}
HourlyEmployee.java
package chap6_24;
public class HourlyEmployee extends Employee{
private double hourlySalary;//每小时的工资
private int hours;//每月工作的小时数
//构造方法
public HourlyEmployee(String name, int month, double hourlySalary, int hours) {
super(name, month);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public HourlyEmployee() {
super();
// TODO Auto-generated constructor stub
}
public double getSlary(int month){
if(hours<160)
return hours*hourlySalary+super.getSlary(month);
else
return 160*hourlySalary+(hours-160)*hourlySalary*1.5+super.getSlary(month);
}
}
SalesEmployee.java
package chap6_24;
public class SalesEmployee extends Employee{
private double sales;//月销售额
private double rate;//提成率
//构造方法
public SalesEmployee(String name, int month, double sales, double rate) {
super(name, month);
this.sales = sales;
this.rate = rate;
}
public SalesEmployee() {
super();
// TODO Auto-generated constructor stub
}
public double getSalary(int month){
return sales*rate+super.getSlary(month);
}
}
BasePlusSalesEmployee.java
package chap6_24;
public class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary;//销售人员的底薪
//构造方法
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double baseSalary) {
super(name, month, sales, rate);
this.baseSalary = baseSalary;
}
public BasePlusSalesEmployee() {
super();
// TODO Auto-generated constructor stub
}
public double getSalary(int month) {
return baseSalary+super.getSalary(month);
}
}
Test.java
package chap6_24;
import java.io.ObjectInputStream.GetField;
public class Test {
public static void main(String[] args) {
SalariedEmployee salariedEmployee = new SalariedEmployee("张三",5,5000);
System.out.println("张三的工资"+salariedEmployee.getSalary(6));
HourlyEmployee h = new HourlyEmployee("李四",3,200,172);
System.out.println("李四的工资"+h.getSlary(3));
SalesEmployee se = new SalesEmployee("王五",4,100000,0.3);
System.out.println("王五的工资"+se.getSalary(4));
BasePlusSalesEmployee b = new BasePlusSalesEmployee("王二",5,20000,0.03,5000);
System.out.println("王二的工资"+b.getSalary(3));
}
}
推荐阅读