2018-12-12
Cola公司的雇员分为以下若干类:(知识点:多态) [必做题]
4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
//4、 Cola公司的雇员分为以下若干类:
//4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
//方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
//
public class ColaEmployee {
public String name;
public int birth;
public double getSalary(int month) {
double s=0;
if(this.birth==month) {
s=100;
}
return s;
}
}
//4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
public class SalariedEmployee extends ColaEmployee {
public double salary_month;
public double getSalary(int month) {
return salary_month+super.getSalary(month);
}
}
//4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,
//每月工作超出160 小时的部分按照1.5 倍工资发放。
//属性:每小时的工资、每月工作的小时数
public class HourlyEmployee extends ColaEmployee {
public double salary_hour;
public double hour;
public double getSalary(int month) {
if(hour>160) {
return salary_hour*160+(hour-160)*salary_hour*1.5+super.getSalary(month);
}else {
return salary_hour*hour+super.getSalary(month);
}
}
}
//4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。
//属性:月销售额、提成率
public class SalesEmployee extends ColaEmployee {
public double sale_month;
public double percent;
public double getSalary(int month) {
return sale_month*percent+super.getSalary(month);
}
}
//4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,
//写一个测试类TestCompany,在main方法,
//把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
public class Company {
public void money(ColaEmployee arr[],int month) {
for(int i=0;i<arr.length;i++) {
double company=arr[i].getSalary(month);
System.out.println(month+"月"+arr[i].name+"的工资是:"+company);
}
}
}
import java.util.Scanner;
public class TestCompany {
public static void main(String[] args) {
Company c=new Company();
Scanner input=new Scanner(System.in);
System.out.println("请输入要查询的月份:");
int m =input.nextInt();
SalariedEmployee ae=new SalariedEmployee();
HourlyEmployee he=new HourlyEmployee();
SalesEmployee se=new SalesEmployee();
ColaEmployee[] ce=new ColaEmployee[] {ae,se,he};
ae.birth=10;
ae.name="王铁夫";
ae.salary_month=1000;
se.sale_month=1000;
se.birth=11;
se.name="孙赫阳";
se.percent=1.5;
he.birth=12;
he.name="孙宸";
he.hour=300;
he.salary_hour=10;
c.money(ce, m);