多态Employee
目录
(3)声明一个子类SalaryEmployee正式工,继承父类Employee
(4)声明一个子类HourEmployee小时工,继承父类Employee
(5)声明一个子类Manager经理,继承SalaryEmployee
(6)声明一个员工数组,存储各种员工,你现在是人事,遍历查看每个人的详细信息,并统计实发工资总额,通知财务准备资金。
(7)从键盘输入当期月份值,如果他是正式工(包括SalaryEmployee和Manager),并且是本月生日的,通知领取生日礼物。
(1)声明一个父类Employee员工类型,
-
有姓名属性,私有化,提供get/set方法
-
public double earning():代表实发工资,返回0.0
-
public String getInfo():显示姓名和实发工资
(2)声明MyDate类型
-
有int类型的年,月,日属性,私有化,提供get/set方法
-
提供public String getInfo(),返回“xxxx年xx月xx日”
(3)声明一个子类SalaryEmployee正式工,继承父类Employee
-
增加属性,double类型的薪资,MyDate类型的出生日期,私有化,提供get/set方法
-
重写方法,public double earning()返回实发工资, 实发工资 = 薪资
-
重写方法,public String getInfo():显示姓名和实发工资、生日
(4)声明一个子类HourEmployee小时工,继承父类Employee
-
增加属性,double类型的工作小时数和每小时多少钱,私有化,提供get/set方法
-
重写方法,public double earning()返回实发工资, 实发工资 = 每小时多少钱 * 小时数
-
重写方法,public String getInfo():显示姓名和实发工资,时薪,工作小时数
(5)声明一个子类Manager经理,继承SalaryEmployee
-
增加属性:奖金比例,私有化,提供get/set方法
-
重写方法,public double earning()返回实发工资, 实发工资 = 薪资 *(1+奖金比例)
-
重写方法,public String getInfo():显示姓名和实发工资,生日,奖金比例
(6)声明一个员工数组,存储各种员工,你现在是人事,遍历查看每个人的详细信息,并统计实发工资总额,通知财务准备资金。
(7)从键盘输入当期月份值,如果他是正式工(包括SalaryEmployee和Manager),并且是本月生日的,通知领取生日礼物。
(1)声明一个父类Employee员工类型
public class Employee {
private String name;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
//实发工资
public double earning(){
return 0.0;
}
//显示信息
public String getInfo(){
return "姓名:"+name+",实发工资:"+earning();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)声明MyDate类型
public class MyDate {
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getInfo(){
return year+"年"+month+"月"+day+"日";
}
}
(3)声明一个子类SalaryEmployee正式工,继承父类Employee
public class SalaryEmployee extends Employee {
private double salary;
private MyDate birthday;
public SalaryEmployee() {
}
public SalaryEmployee(String name, double salary, MyDate birthday) {
super(name);
this.salary = salary;
this.birthday = birthday;
}
//为子类添加日期的构造器创建的
public SalaryEmployee(String name, double salary) {
super(name);
this.salary = salary;
}
public SalaryEmployee(String name, double salary, int year, int month, int day) {
super(name);
this.salary = salary;
//!!!注意
//新建一个MyDate对象,传正式工的生日
this.birthday = new MyDate(year, month, day);
}
@Override
public double earning() {
return this.salary;
}
@Override
public String getInfo() {
return super.getInfo() + ",生日是:" + birthday.getInfo();
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public MyDate getBirthday() {
return birthday;
}
}
(4)声明一个子类HourEmployee小时工,继承父类Employee
public class HourEmployee extends Employee{
private double workHours;
private double moneyHour;
public HourEmployee() {
}
public HourEmployee(String name, double workHours, double moneyHour) {
super(name);
this.workHours = workHours;
this.moneyHour = moneyHour;
}
@Override
public double earning() {
return workHours*moneyHour;
}
@Override
public String getInfo() {
return super.getInfo()+",时薪是:"+earning()+",工作的小时数是:"+workHours;
}
public double getWorkHours() {
return workHours;
}
public void setWorkHours(double workHours) {
this.workHours = workHours;
}
public double getMoneyHour() {
return moneyHour;
}
public void setMoneyHour(double moneyHour) {
this.moneyHour = moneyHour;
}
}
(5)声明一个子类Manager经理,继承SalaryEmployee
public class Manager extends SalaryEmployee {
private double bonusRatio;
@Override
public double earning() {
return super.earning() * (1 + bonusRatio);
}
@Override
public String getInfo() {
return super.getInfo() + ",奖金比例是:" + bonusRatio;
}
public Manager() {
}
public Manager(String name, double salary, MyDate birthday, double bonusRatio) {
super(name, salary, birthday);
this.bonusRatio = bonusRatio;
}
// public Manager(String name,double salary, int year, int month ,int day, double bonusRate){
// super(name, salary);
// this.bonusRatio = bonusRatio;
// //新建一个MyDate对象,传生日
// MyDate myDate =new MyDate(year,month,day);
// }
public double getBonusRatio() {
return bonusRatio;
}
public void setBonusRatio(int bonusRatio) {
this.bonusRatio = bonusRatio;
}
}
(6)声明一个员工数组,存储各种员工,你现在是人事,遍历查看每个人的详细信息,并统计实发工资总额,通知财务准备资金。
public class TestEmployee {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new HourEmployee("小刘", 80, 20);
MyDate date1 = new MyDate(2000, 1, 25);
staff[1] = new Manager("小丽", 8000, date1, 0.2);
// staff[1] = new Manager("小丽", 8000, 2000, 1, 25, 0.2);
staff[2] = new SalaryEmployee("小娜", 10000, 2001, 9, 25);
//遍历查看每个人的信息
for (int i = 0; i < staff.length; i++) {
System.out.println(staff[i].getInfo());
}
//求总工资
double salarySum = 0;
for (int i = 0; i < staff.length; i++) {
salarySum += staff[i].earning();
}
System.out.println("3位员工的总工资已统计,请财务准备好资金:" + salarySum + "!!!");
}
}
(7)从键盘输入当期月份值
public class TestBirthday {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Employee[] staff = new Employee[3];
staff[0] = new HourEmployee("小刘", 80, 20);
MyDate date1 = new MyDate(2000, 1, 25);
staff[1] = new Manager("小丽", 8000, date1, 0.2);
staff[2] = new SalaryEmployee("小娜", 10000, 2001, 9, 25);
//&& !(emp instanceof Manager) 可以不写
System.out.print("请输入当前月份值:");
int month = scan.nextInt();
for (int i = 0; i < staff.length; i++) {
if (!(staff[i] instanceof SalaryEmployee)) {
System.out.println(staff[i].getName() + ",很遗憾他不是正式员工!");
} else {
//强转类型 ((SalaryEmployee) staff[i]).getBirthday().getMonth() == month
if (((SalaryEmployee) staff[i]).getBirthday().getMonth() == month) {
System.out.println(staff[i].getName() + ",生日快乐!请通知她生日当天到人事部领取礼物!");
continue;
} else {
System.out.println(staff[i].getName() + ",很遗憾本月不是她的生日~");
}
}
}
}
}