class Employee{
private String name;//定义姓名name并私有化属性
private int month;//定义生日月份month并私有化属性
public Employee(){}//无参构造器
public Employee(String name,int month){//有参构造方法
this.name = name;//给属性name初始化赋值
this.month = month;//给属性month初始化赋值
}
//获取属性name的方法
public String getName(){
return name;//返回name属性
}
//获取属性month的方法
public int getMonth(){
return month;//返回month属性
}
//给属性name赋初始值
public void setName(String name){
this.name = name;//本类中的属性name
}
//给属性month赋初始值
public void setMonth(int month){
this.month = month;//本类中的属性month
}
//创建一个方法getSalary()用来计算工资,参数month是月份,如果当月是员工生日,奖 励100元
public double getSalary(int month){
double salary = 0;//定义工资变量
//判断当前月份是否是员 工的生日月份,如果是奖励100元
if(this.month == month){
salary = salary + 100;
}
return salary;//返回工资salary
}
}
class SalariedEmployee extends Employee{
private double monthSalary; //封装monthSalary属性
public SalariedEmployee(){} //无参构造方法
//有参构造方法 参数 姓名 生日月份 月薪
public SalariedEmployee(String name,int month,double monthSalary){
super(name,month); //调用父类有参构造方法
this.monthSalary = monthSalary; //为属性monthSalary初始化赋值
}
//获取monthSalary的值
public double getMonthSalary(){
return monthSalary;
}
//给monthSalary赋值
public void setMonthSalary(double monthSalary){
this.monthSalary = monthSalary;
}
//覆盖父类中的方法
public double getSalary(int month){
double salary = monthSalary+super.getSalary(month); //定义工资变量
return salary;
}
}
class HourlyEmployee extends Employee{
private double hourlySalary; //定义属性hourlySalary每小时的工资
private int hours; //定义属性hours每月工作的小时数
public HourlyEmployee(){} //无参构造方法
//有参构造方法 参数 姓名 生日月份 每小时的工资 每月工作的小时数
public HourlyEmployee(String name,int month,double hourlySalary,int hours){
super(name,month); //调用父类有参构造方法
this.hourlySalary = hourlySalary ; //为属性hourlySalary初始化赋值
this.hours = hours; //为属性hours 初始化赋值
}
public double getHourlySalary(){ //获取hourlySalary的值
return hourlySalary;
}
public int getHours(){ //获取hours的值
return hours;
}
//定义set方法设置hourlySalary hours的值
public void setHourlySalary(double hourlySalary){
this.hourlySalary =hourlySalary;
}
public void setHourly(int hours){
this.hours = hours;
}
//覆盖父类方法
public double getSalary(int month){
if(hours < 0){ //如果工作小时数小于0 输出数据错误
System.out.println("数据错误");
return 0;
}
//小于160个小时的 按照每个月的工作小时数乘以每小时的工资
else if(hours <= 160)
return hourlySalary*hours+super.getSalary(month);
//超出160个小时的小时数 按照1.5倍计算
else return hourlySalary*160+hourlySalary*1.5*(hours-160)+super.getSalary(month);
}
}
class SalesEmployee extends Employee{
private double sales ; //定义销售额sales
private double rate; //定义提成率rate
public SalesEmployee(){}
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 double getRate(){
return rate;
}
public void setSales(double sales){
this.sales = sales;
}
public void setRate(double rate){
this.rate = rate;
}
public double getSalary(int month){
return this.getSales()*this.getRate()+super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary; //定义基础工资baseSalary
//无参构造方法
public BasePlusSalesEmployee(){}
//有参构造方法
public BasePlusSalesEmployee(String name,int month,double sales,double rate,double baseSalary){
super(name,month,sales,rate);
this.baseSalary = baseSalary;
}
//get/set方法对私有属性的调用和设置
public double gatBaseSalary(){
return baseSalary;
}
public void setBaseSalary(){
this.baseSalary = baseSalary;
}
public double getSalary(int month){
return baseSalary+super.getSalary(month);
}
}
//定义一个测试类
public class Example{
public static void main(String[] args){
/*Employee e1=new Employee("张三",1);
System.out.println("Name:"+e1.getName()+" ,Salary:"+Math.round(e1.getSalary(2)));
SalariedEmployee s1=new SalariedEmployee("张三",1,6000);
System.out.println("Name:"+s1.getName()+" ,Salary:"+Math.round(s1.getSalary(2)));
HourlyEmployee h1=new HourlyEmployee("李四",2,50,180);
System.out.println("Name:"+h1.getName()+" ,Salary:"+Math.round(h1.getSalary(2)));
System.out.println("Name:"+s1.getName()+" ,Salary:"+Math.round(s1.getSalary(1)));
SalesEmployee s2=new SalesEmployee("王五",3,6500,0.15);
System.out.println("Name:"+s2.getName()+" ,Salary:"+Math.round(s2.getSalary(2)));
BasePlusSalesEmployee b1=new BasePlusSalesEmployee("赵六",4,5000,0.15,2000);
System.out.println("Name:"+b1.getName()+" ,Salary:"+Math.round(b1.getSalary(2)));*/
Employee[] employee = {new SalariedEmployee("张三",1,6000),new HourlyEmployee("李四",2,50,180),new SalesEmployee("王五",3,6500,0.15),new BasePlusSalesEmployee("赵六",4,5000,0.15,2000)};
//打印每个员工的工资
for(int i = 0; i < employee.length ;i++)
//System.out.println(Math.round(employee[i].getSalary(10)));
System.out.println("Name:"+employee[i].getName()+" ,Salary:"+Math.round(employee[i].getSalary(10)));
}
}
//声明一个Employee类型的数组,并创建不同子类型的对象
//Employee[] employee = {new SalariedEmployee("张三",1,6000),new HourlyEmployee("李四",2,50,180),new SalesEmployee("王五",3,6500,0.15),new BasePlusSalesEmployee("赵六",4,5000,0.15,2000)};
//打印每个员工的工资
//for(int i = 0; i < employee.length ;i++)
//System.out.println(Math.round(employee[i].getSalary(10)));
//}