学习记录分享

本文介绍了一个员工薪资计算系统,该系统包含多种类型的员工,如固定薪资员工、按小时计薪员工、销售员工以及底薪加提成员工。通过实例化不同类型的员工并调用其getSalary方法,可以计算出每位员工的月薪,考虑到加班、销售提成和生日月份的额外奖金。

在这里插入图片描述
在这里插入图片描述

public class Test17 {
	public static void main(String[] args) {//主方法
		SalariedEmployee a = new SalariedEmployee("a",1,3000.0);//构造对象a
		HourlyEmployee b = new HourlyEmployee("b",2,20.0,180);//构造对象b
		SalesEmployee c = new SalesEmployee("c",3,40000.0,0.2);//构造对象c
		BasePlusSalesEmployee d = new BasePlusSalesEmployee("d",4,35000.0,0.2,3000.0);//构造对象d
		System.out.println("a的3月份的工资为:"+a.getSalary(3));//打印对象a的3月份工资
		System.out.println("b的3月份的工资为:"+b.getSalary(3));//打印对象b的3月份工资
		System.out.println("c的3月份的工资为:"+c.getSalary(3));//打印对象c的3月份工资
		System.out.println("d的3月份的工资为:"+d.getSalary(3));//打印对象d的3月份工资
	}
}
class Employee{//员工父类
	private String name;//姓名(私有属性)
	private int month;//生日月份(私有属性)
	public Employee() {}//无参构造
	public Employee(String name, int month) {//有参构造
		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(month == this.month) {
			return 100;
		}else {
			return 0;
		}
	}
}
class SalariedEmployee extends Employee{//拿固定工资的员工,继承Employee
	private double salar;//固定工资(私有属性)
	public SalariedEmployee() {//无参构造
	}
	public SalariedEmployee(String name, int month,double salar) {//有参构造
		super(name,month);//调用父类有参构造
		this.salar = salar;//子类属性赋值
	}
	public double getSalary(int month) {//月薪计算
		return this.salar+super.getSalary(month);
	}
	//属性访问
	public double getSalar() {
		return salar;
	}
	public void setSalar(double salar) {
		this.salar = salar;
	}
}
class HourlyEmployee extends Employee{//按小时拿工资的员工,继承Employee
	private double hourlySalar;//每小时的工资(私有属性)
	private int hours;//每月工作小时数(私有属性)
	public double getSalary(int month) {//月薪计算
		if(this.hours>160) {
			return hourlySalar*160+(hours-160)*hourlySalar*1.5+super.getSalary(month);
		}else {
			return hourlySalar*+super.getSalary(month);
		}
	}
	public HourlyEmployee() {//无参构造
	}
	public HourlyEmployee(String name, int month,double hourlySalar,int hours) {//有参构造
		super(name,month);//调用父类有参构造
		this.hourlySalar = hourlySalar;//子类属性赋值
		this.hours = hours;//子类属性赋值
	}
	//属性访问
	public double getHourlySalar() {
		return hourlySalar;
	}
	public void setHourlySalar(double hourlySalar) {
		this.hourlySalar = hourlySalar;
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
}
class SalesEmployee extends Employee{//按提成拿工资的员工,继承Employee
	private double sales;//月销售额(私有属性)
	private double rate;//提成率(私有属性)
	public double getSalary(int month) {//月薪计算
		return sales*rate+super.getSalary(month);
	}
	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 void setSales(double sales) {
		this.sales = sales;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
}
class BasePlusSalesEmployee extends SalesEmployee{//按固定底薪和销售提成拿工资的员工,继承SalesEmployee
	private double baseSalary;//固定底薪(私有属性)
	public double getSalary(int month) {//月薪计算
		return baseSalary+super.getSalary(month);
	}
	public BasePlusSalesEmployee() {//无参构造
	}
	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;
	}
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值