1.设计父类一员工类。子类:工人类(Worker),农民类(Peasant), 教师类(Teacher),科学家类
(Scientist),服务生类(Waiter)。
(1)其中工人,农民,服务生只有基本工资
(2)教师除基本工资外,还有课酬(元/天)
(3)科学家除基本工资外,还有年终奖
(4)编写一个测试类,将各种类型的员工的全年工资打印出来
Homework5
package com.hspedu.homework.homework05;
public class Homework5 {
public static void main(String[] args) {
Worker jack = new Worker("jack", 100);
jack.setSalMonth(10);
jack.printSal();
Peasant smith = new Peasant("smith", 150);
smith.printSal();
Teacher amir = new Teacher("Amir", 300);
amir.setClassDays(360);
amir.setClassSal(100);
amir.printSal();
Scientist scientist = new Scientist("钟南山", 20000);
scientist.setBonus(2000000);
scientist.printSal();
}
}
Employee
package com.hspedu.homework.homework05;
public class Employee {
private String name;
private double salary;
private int salMonth = 12;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getSalMonth() {
return salMonth;
}
public void setSalMonth(int salMonth) {
this.salMonth = salMonth;
}
public void printSal(){
System.out.println(name + " 年工资是:" + (salary*salMonth));
}
}
Worker
package com.hspedu.homework.homework05;
public class Worker extends Employee{
public Worker(String name, double salary) {
super(name, salary);
}
@Override
public void printSal() {
System.out.print("工人");
super.printSal();
}
}
Peasant
package com.hspedu.homework.homework05;
public class Peasant extends Employee{
public Peasant(String name, double salary) {
super(name, salary);
}
@Override
public void printSal() {
System.out.print("农民");
super.printSal();
}
}
Teacher
package com.hspedu.homework.homework05;
public class Teacher extends Employee{
private int classDays;
private double classSal;
public Teacher(String name, double salary) {
super(name, salary);
}
public int getClassDays() {
return classDays;
}
public void setClassDays(int classDays) {
this.classDays = classDays;
}
public double getClassSal() {
return classSal;
}
public void setClassSal(double classSal) {
this.classSal = classSal;
}
@Override
public void printSal() {
System.out.println("老师" + getName() + " 年工资是:"
+(getSalary() * getSalMonth() + classSal * classDays) );
}
}
Scientist
package com.hspedu.homework.homework05;
public class Scientist extends Employee{
private double bonus;
public Scientist(String name, double salary) {
super(name, salary);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void printSal() {
System.out.println("科学家" + getName() +" 年工资是:" +
(getSalary()*getSalMonth()+getBonus()));
}
}