多态 继承 练习题 计算某个月四种对象的工资

public class Text17 {

	public static void main(String[] args) {
	Employee[] s = new Employee[4];
	s[0] = new SalariedEmployee("Tom",4,4000.0);
	s[1] = new HourlyEmployee("marry",5,90,50);
	s[2] = new SalesEmployee("lisa",6,50000,0.4);
	s[3] = new BasePlusSalesEmployee("susan",4,40000,0.3,1000);
		for(int i = 0; i<s.length; i++){
			System.out.println(s[i].getName()+"的工资为"+s[i].getSalary(5));
		}//遍历打印出各位员工的工资
	
	}

}

class Employee {//公司父类
	private String name;//姓名
	private int month;//生日月份

	public Employee(){}
	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;
	}

	//如果员工过生日,公司会额外奖励100元
	public double getSalary(int month) {
		if (month == this.month) {
			return 100.0;
		} else {
			return 0.0;
		}
	}
}

//拿固定工资的员工
class SalariedEmployee extends Employee {
	private double salary;//月薪
	
	public SalariedEmployee(String name, int month, double salary) {
		super(name, month);
		this.salary = salary;
	}

	public double getSalary(int month) {
		double birthday = super.getSalary(month);
		return salary + birthday;
	}
}

//小时工,每月工作超出160个小时按照1.5倍工资算
class HourlyEmployee extends Employee {
	private int hours;//每个月的小时数
	private double hourlySalary;//每个小时的工资
	
	public HourlyEmployee(String name, int month,int hours, double hourlySalary) {
		super(name, month);
		this.hours = hours;
		this.hourlySalary = hourlySalary;
	}
	
	public double getSalary(int month) {
		double birthday = super.getSalary(month);//判断是否有奖金
		if (hours > 0 && hours <= 160) {//160个小时内的工资
			return hours * hourlySalary + birthday;
		} else {//160个小时外的工资
			return 160 * hourlySalary + (hours - 160) * hourlySalary * 1.5 + birthday;
		}
	}
}

//销售员工
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 getSalary(int month) {
		double birthday = super.getSalary(month);//判断是否有奖金
		return sales * rate + birthday;
	}
}

//有固定的底薪的销售人员,工资由底薪加销售提成
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 double getSalary(int month) {
		double birthday = super.getSalary(month);//引用其父类销售员工方法计算其提成和是否有生日金
		return baseSalary + birthday;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值