1.编写一个Employee类,声明为抽象类,包含如下三个属性:name,id,salary。
提供必要的构造器和抽象方法:work()。
对于Manager类来说,他既是员工,还具有奖金(bonus)的属性。
请使用继承的思想,设计CommonEmployee类和Manager类,要求类中提供必要的方法进行属性访问。
Mamager类:
package exer3;
public class Manager extends Employee {
private double bonus;// 奖金
public Manager(double bonus) {
super();
this.bonus = bonus;
}
public Manager(String name, int id, double salary, double bonus) {
super(name, id, salary);
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("管理员工,提高效率");
}
}
CommonEmployee类:
package exer3;
public class CommonEmployee extends Employee {
@Override
public void work() {
System.out.println("员工在一线车间工作");
}
}
EmployeeTest类:
package exer3;
public class EmployeeTest {
public static void main(String[] args) {
//多态
Manager manager = new Manager("库克",1001,500,50000);
manager.work();
CommonEmployee CommonEmployee = new CommonEmployee();
CommonEmployee.work();
}
}
输出:
管理员工,提高效率
员工在一线车间工作
2.编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
实验说明:
(1)定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();
toString()方法输出对象的name,number和birthday。
(2)MyDate类包含:
private成员变量year,month,day ;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。该类包括:private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输出员工类型信息及员工的name,number,birthday。
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的
员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;
toString()方法输出员工类型信息及员工的name,number,birthday。
(5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
提示:
//定义People类型的数组People c1[]=new People[10];
//数组元素赋值
c1[0]=new People(“John”,“0001”,20); c1[1]=new People(“Bob”,“0002”,19);
//若People有两个子类Student和Officer,则数组元素赋值时,可以使父类类型的数组元素指向子类。 c1[0]=new Student(“John”,“0001”,20,85.0); c1[1]=new Officer(“Bob”,“0002”,19,90.5);
Employee类:
package exer4;
public abstract class Employee {
private String name;
private int number;
private MyDate birthday;
public Employee(String name, int number, MyDate birthday) {
super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public abstract double earnings();
@Override
public String toString() {
return "name=" + name + ", number=" + number + ", birthday=" + birthday;
}
}
MyDate类:
package exer4;
public class MyDate {
private int year;
private int month;
private int day;
public MyDate() {
super();
}
public MyDate(int year, int month, int day) {
super();
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 toDateString() {
return year + "年" + month + "月" + day + "日";
}
}
SalariedEmployee类:
package exer4;
public class SalariedEmployee extends Employee {
private int monthlySalary;// 月工资
public SalariedEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public SalariedEmployee(String name, int number, MyDate birthday, int monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
public String toString() {
return "SalariedEmployee[" + super.toString() + "]";
}
}
HourlyEmployee类:
package exer4;
public class HourlyEmployee extends Employee {
private int wage;// 每小时的工资
private int hour;// 月工作的小时数
public HourlyEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
@Override
public double earnings() {
return wage * hour;
}
public String toString() {
return "HourlyEmployee[" + super.toString() + "]";
}
}
PatrollSystem类:
package exer4;
import java.util.Scanner;
public class PatrollSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入当月的月份:");
int month = scanner.nextInt();
Employee[] emps = new Employee[2];
emps[0] = new SalariedEmployee("小明", 1002, new MyDate(1998, 5, 6), 10000);
emps[1] = new HourlyEmployee("小王", 2021, new MyDate(2020, 10, 8), 12, 6);
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
double salary = emps[i].earnings();
System.out.println("月工资为:" + salary);
if(month == emps[i].getBirthday().getMonth()) {
System.out.println("生日快乐!奖励100元");
}
}
}
}
输出:
请输入当月的月份:
5
SalariedEmployee[name=小明, number=1002, birthday=exer4.MyDate@3b81a1bc]
月工资为:10000.0
生日快乐!奖励100元
HourlyEmployee[name=小王, number=2021, birthday=exer4.MyDate@2c6a3f77]
月工资为:72.0