3、 Cola公司的雇员分为以下若干类:(知识点:多态)
3.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
3.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
3.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
3.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
3.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
public class ColaEmployee {
private String name;
private int month;
public ColaEmployee ( String name, int month) {
super ( ) ;
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;
}
public void getSalary ( int month) {
}
}
public class SalariedEmployee extends ColaEmployee {
private double salary;
public SalariedEmployee ( String name, int month, double salary) {
super ( name, month) ;
this . salary = salary;
}
public double getSalary ( ) {
return salary;
}
public void setSalary ( double salary) {
this . salary = salary;
}
@Override
public void getSalary ( int month) {
if ( month == getMonth ( ) ) {
salary += 100 ;
}
System. out. println ( getName ( ) + "在" + getMonth ( ) + "月的工资为:" + salary) ;
}
}
public class HourlyEmployee extends ColaEmployee {
private double hourSalary;
private int hour;
public HourlyEmployee ( String name, int month, double hourSalary, int hour) {
super ( name, month) ;
this . hourSalary = hourSalary;
this . hour = hour;
}
@Override
public void getSalary ( int month) {
double s = 0 , x = hour;
if ( month == getMonth ( ) ) {
s += 100 ;
}
if ( x > 160 ) {
s += ( x - 160 ) * hourSalary* 1.5 ;
x -= 160 ;
}
s += x* hourSalary;
System. out. println ( getName ( ) + "在" + getMonth ( ) + "月的工资为:" + s) ;
}
}
public class SalesEmployee extends ColaEmployee {
private double sales;
private double rate;
public SalesEmployee ( String name, int month, double sales, double rate) {
super ( name, month) ;
this . rate = rate;
this . sales = sales;
}
@Override
public void getSalary ( int month) {
double s = 0 ;
if ( month == getMonth ( ) ) {
s += 100 ;
}
s += sales* rate;
System. out. println ( getName ( ) + "在" + getMonth ( ) + "月的工资为:" + s) ;
}
}
public class Company {
public void showSalary ( ColaEmployee c, int month) {
c. getSalary ( month) ;
}
}
public class TestCompany {
public static void main ( String[ ] args) {
Company company = new Company ( ) ;
ColaEmployee[ ] c= new ColaEmployee [ 3 ] ;
c[ 0 ] = new SalariedEmployee ( "c1" , 12 , 2000 ) ;
c[ 1 ] = new HourlyEmployee ( "c2" , 3 , 80 , 170 ) ;
c[ 2 ] = new SalesEmployee ( "c3" , 5 , 50000 , 0.2 ) ;
for ( ColaEmployee i : c) {
company. showSalary ( i, 12 ) ;
}
}
}