一、(封装 继承 多态)某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,
属性:员工的姓名和生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放
属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定
属性:月销售额、提成率
BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分
属性:底薪。
要求:
- 创建Employee、SalariedEmployee、SalesEmployee、BasePlusSalesEmployee四个类的对象各一个
- 并调用父类getSalary(int money)方法计算某个月这四个对象各自的工资。
二、(接口)公司给SalariedEmployee 每月另外发放2000元加班费和,给BasePlusSalesEmployee发放1000元加班费改写原有代码,加上以上逻辑。并写一个方法,打印出本月公司总共发放多少加班费。
public class TestEmployee {
public static void main(String[] args) {
Employee[] e=new Employee[4];
e[0]=new SalariedEmployee("tom",3,6000.0);
e[1]=new HourlyEmployee("jack",3,30.0,200);
e[2]=new SalesEmployee("kitty",3,10000,0.7);
e[3]=new BasePlusSalesEmployee("janie",3,10000,0.7,1000);
double a=0.0;//全公司加班费
for (int i = 0; i < e.length; i++) {
System.out.println(e[i].getName()+" "+e[i].getSalary(3));//工资
if(e[i] instanceof OverTime) {
a+=(( OverTime)e[i]).addSalary();
}
}
System.out.println("全公司加班费:"+a);
}
}
interface OverTime{//加班费
double addSalary();
}
class Employee{
private String name;
private int month;
public Employee(String name, int month) {
super();
this.name = name;
this.month = month;
}
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) {
if(this.getMonth()==month) {
return 100.0;
}
return 0.0;
}
}
class SalariedEmployee extends Employee implements OverTime{
private double salary;
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double addSalary() {//加班费
return 2000.0;
}
public double getSalary(int month) {
return this.salary+super.getSalary(month)+this.addSalary();
}
}
class HourlyEmployee extends Employee{
private double hourSalary;
private int hours;
public HourlyEmployee(String name, int month, double hourSalary, int hours) {
super(name, month);
this.hourSalary = hourSalary;
this.hours = hours;
}
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public double getSalary(int month) {
double salary=0.0;
if(hours>160) {
salary=160*hourSalary+(hours-160)*1.5*hourSalary;
}else {
salary=hours*hourSalary;
}
return salary+super.getSalary(month);
}
}
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 double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getSalary(int month) {
return sales*rate+super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee implements OverTime{
double baseSalary;
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double baseSalary) {
super(name, month, sales, rate);
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double addSalary() {//加班费
return 1000.0;
}
public double getSalary(int month) {
return baseSalary+super.getSalary(month)+this.addSalary();
}
}
运行结果: