只是为了记录学习----写在前面
l. Employee :这是所有员工总的父类。
1).属性∶员工的姓名,员工的生日月份。
2)、方法:getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外
奖励100元。
ll. SalariedEmployee : Employee 的子类,拿固定工资的员工。
1).属性∶月薪。
IlI. HourlyEmployee : Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照
1.5倍工资发放。
1).属性∶每小时的工资、每月工作的小时数。
IV. SalesEmployee : Employee 的子类,销售,工资由月销售额和提成率决定。
1).属性︰月销售额、提成率。
V. BasePlusSalesEmployee : SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
1).属性︰底薪。
要求︰
l.创建SalariedEmployee、HourlyEmployee、SaleEmployee.
BasePlusSalesEmployee四个类的对象各一个。
lI.并调用父类getSalary(int money)方法计算某个月这四个对象各自的工资。
package com.lx.homework;
public class Employee {
private String name;
private int month; //员工生日月份
// 根据参数月份来确定工资,若该月是员工生日则额外奖励100元
public double getSalary(int month){
// 三目适合简单的运算并且返回是数字时。往这方面考虑用一下
return month == this.month ? 100 : 0.0;
}
// 无参构造
public Employee() {
}
//有参构造,后面用子类用super(参数)调用父类有参构造因此要记得加上无参构造
// why用super(参数)调用父类有参构造? 因为我们后面创建子类对象时
// 为了方便直接在对象中设置参数了 167行注释了哟
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;
}
}
//拿固定工资的员工
class SalariedEmployee extends Employee{
private double salary;
public SalariedEmployee() {
}
public SalariedEmployee(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public double getSalary(int month) {
// super是父类对象 ,
// 可以调用父类的属性super.属性 和方法super.方法();
return super.getSalary(month) + salary;
}
}
//按小时拿工资的员工
class HourEmployee extends Employee{
private double hourlySalary;
private int hours;
public HourEmployee() {
}
public HourEmployee(String name, int month, double hourlySalary, int hours) {
// 调用父类的有参构造
super(name, month);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getHourlySalary() {
return hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
@Override
public double getSalary(int month) {
// double salary = hours > 160 ? 160 * hourlySalary + (hours - 160) * 1.5 *hourlySalary : hours * hourlySalary;
// System.out.println(salary);
return (hours > 160 ? 160 * hourlySalary + (hours - 160) * 1.5 *hourlySalary : hours * hourlySalary)+ super.getSalary(month) ;
}
}
//销售员工
class SalesEmployee extends Employee{
private double sales; //月销售额
private double 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 void setSales(double sales) {
this.sales = sales;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + rate * sales;
}
}
//有底薪的员工
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary;
public BasePlusSalesEmployee() {
}
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double baseSalary) {
// 调用父类的有参构造
// 由于的BasePlusSalesEmployee父类又继承其他父类
// 所以参数里面会带着它父类所继承的父类的参数(就是name 和 month辣)
super(name, month, sales, rate);
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
@Override
public double getSalary(int month) {
// super引用的是父类(即SalesEmployee 这时的super也把销售提成传进来辣)
return super.getSalary(month) + baseSalary ;
}
}
class TestEmployee{
public static void main(String[] args) {
// 固定工资员工获取工资
SalariedEmployee salariedEmployee = new SalariedEmployee();
// 这是子类继承父类并可以使用父类的属性和方法
salariedEmployee.setName("小固");
salariedEmployee.setMonth(1);
salariedEmployee.setSalary(1000);
System.out.println(salariedEmployee.getSalary(1));
//有底薪的员工获取工资(这是有缺陷获取薪资,因为当时没有考虑销售提成,在下面写完整了)
BasePlusSalesEmployee basePlusSalesEmployee = new BasePlusSalesEmployee();
// 再一次尝试用子类继承父类并使用父类的属性和方法
basePlusSalesEmployee.setName("小鸡");
basePlusSalesEmployee.setMonth(1); //有底薪员工的生日月份
basePlusSalesEmployee.setBaseSalary(500); //设置底薪
System.out.println(basePlusSalesEmployee.getSalary(1)); //传入月份计算得到薪资
// 重复上述代码计算薪资你有没有嫌得太过麻烦。我们选择造创建对象时就进行赋值
HourEmployee hourEmployee = new HourEmployee("小时",1,10,170);
// 调用小时工的获取薪资的方法计算薪资
System.out.println(hourEmployee.getSalary(1));
// 由于我们已经使用super(name,month)获取父类的的构造方法
//获取 小时 的名字 由于我们用super(参数)调用了父类的有参构造
// 所以创建对象时已经把值也传入父类
// 而子类继承了父类的方法 故子类可以使用父类的属性和方法(这就是继承的作用)
System.out.println(hourEmployee.getName());
// 销售员工薪资获取
SalesEmployee salesEmployee = new SalesEmployee("小销",1,1800,0.1);
// 调用销售员工计算薪资的方法获取薪资
System.out.println(salesEmployee.getSalary(1));
//有底薪的员工获取工资(这是有完整获取薪资)
BasePlusSalesEmployee basePlusSalesEmployee1 = new BasePlusSalesEmployee("坤坤",1,2400,0.5,200);
// 调用有底薪员工计算薪资的方法获取薪资
System.out.println(basePlusSalesEmployee1.getSalary(2));// 100 + 200 + 1200 = ?
System.out.println(basePlusSalesEmployee1.getName());
}
}