(1)编写抽象基类Employee,该类包含姓名和工号两个字段及相应的属性、构造函数以及用来求工资的抽象函数Salary();
package T2;
public abstract class Employee {
private String name;
private int id;
public Employee() {
}
public Employee(String name, int id) {
this.name = name;
this. id = id;
}
public abstract void Salary();
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
(2)钟点工类Employee1继承Employee类,该类新增两个字段wage和hours,分别表示每小时的工资和工作时间,对应的属性能判断出数据的合理性(即wage>0, hours>0);在该类中重写Salary();
package T2;
public class Employee1 extends Employee {
double wage;
int hours;
public Employee1(){}
public Employee1(double wage,int hours){
if (wage>0&&hours>0){
this.wage=wage;
this.hours=hours;
}else {
System.out.println("输入数据不合格");
}
}
@Override
public void Salary() {
System.out.println("钟点工的工资为:"+wage*hours);
}
}
(3)正式员工类Employee2类采用底薪+提成的方式计薪,该类继承Employee类,且新增三个字段,分别表示底薪、总销售额和佣金百分比;在该类中重写Salary();
package T2;
public class Emloyee2 extends Employee {
double wage;
double sales;
double percent;
public Emloyee2(){}
public Emloyee2(double wage,double sales,double percent){
this.wage=wage;
this.sales=sales;
this.percent=percent;
}
@Override
public void Salary(){
double salary=wage+sales*percent;
System.out.println("正式员工的工资为;"+salary);
}
}
(4)在主函数中创建两个派生类的对象,完成对象初始化,利用多态的方式,输出两类人员的工资。
package T2;
public class Deom01 {
public static void main(String[] args) {
Employee e1=new Employee1(80,10);
e1.Salary();
Employee e2=new Emloyee2(3000,10000,0.3);
e2.Salary();
}
}