这周老师布置了一个Java的小作业,所以就把代码分享给大家吧!
一、 实验目的:
1、理解封装机制,掌握各种修饰符的使用。
2、理解继承机制,掌握继承的应用。
3、体会并掌握 this、super 的使用。
二、实验内容:
某公司的员工分为以下若干类:
1、Employee:所有员工总的父类:
属性:姓名,出生月份。
方法:getSalary (int month)如果该月员工过生日,公司额外奖励 100 元。
2、SalariedEmployee:Employee 的子类,拿固定工资的员工。
属性:姓名,出生月份、月薪
方法:重写父类的 getSalary(),计算出应发工资后应加上 super.getSalary()。
3、HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出 160 小时 的部分按照 1.5 倍工资发放。
属性:姓名,出生月份,每月工作的小时数、每小时的工资。
方法:重写父类的 getSalary(),计算出应发工资后应加上 super.getSalary()。
4、SalesEmployee:Employee 的子类,销售人员,工资由月销售额和提成率决定。
属性: 月属性:姓名,出生月份,销售额、提成率。
方法:重写父类的 getSalary()方法,计算出应发工资后应加上 super.getSalary()。
5、BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底 薪加上销售提成部分。
属性:姓名,出生月份,销售额、提成率,底薪。 方法:重写父类 SalesEmployee 的 getSalary()方法,计算出应发工资后应加上 super.getSalary()。
package Employee;
public class Employee{
private String name;
private int month;
Employee(){}
Employee(String name, int month){
this.name = name;
this.month = month;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setMouth(int month){
this.month = month;
}
public int getMouth(){
return month;
}
public double getSalary(int month){
if (month == this.month)
{
return 100;
}
else
{
return 0;
}
}
}
//以下是SalariedEmployee类
package Employee;
class SalariedEmployee extends Employee
{
private double monthSalary; //月薪
public SalariedEmployee() {}
public SalariedEmployee(String name, int month, int monthSalary) {
super(name, month);
this.monthSalary = monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary = monthSalary;
}
public double getMonthSalary() {
return monthSalary;
}
@Override
public double getSalary(int month){
return monthSalary + super.getSalary(month);
}
}
///以下是BasePlusSalesEmployee类
package Employee;
public class BasePlusSalesEmployee extends SalesEmployee {
double baseSalary;
BasePlusSalesEmployee(){}
BasePlusSalesEmployee(String name,int month,int monthSales, double rate,double baseSalary){
super(name,month,monthSales,rate);
this.baseSalary = baseSalary;
}
public void setBaseSalary(double baseSalary){
this.baseSalary = baseSalary;
}
public double getBaseSalary(){
return baseSalary;
}
@Override
public double getSalary(int month){
return baseSalary+super.getSalary(month);
}
}
/以下是HourlyEmployee类
package Employee;
public class HourlyEmployee extends Employee{
private int hour;
private double hourPay;
HourlyEmployee(){}
HourlyEmployee(String name,int month,int hour, double hourPay){
super(name, month);
this.hour = hour;
this.hourPay = hourPay;
}
public void setHour(int hour){
this.hour = hour;
}
public int getHour(){
return hour;
}
public void setHourPay(double hourPay){
this.hourPay = hourPay;
}
public double getHourPay(){
return hourPay;
}
@Override
public double getSalary(int month){
if (hour > 160)
{
return (float) (hourPay * 160 + (hour - 160) * hourPay * 1.5
+ super.getSalary(month));
}
else
{
return hourPay * hour + super.getSalary(month);
}
}
}
///以下是SalesEmployee类
package Employee;
public class SalesEmployee extends Employee{
private int monthSales;
private double rate;
SalesEmployee(){};
SalesEmployee(String name,int month,int monthSales, double rate){
super(name,month);
this.monthSales = monthSales;
this.rate = rate;
}
@Override
public double getSalary(int month){
return monthSales * rate + super.getSalary(month);
}
}
以下是Test运行
package Employee;
public class EmployeeTest{
public static void main(String[] args){
Employee a[] = new Employee[4];
a[0] = new SalariedEmployee("张三", 2, 1000);
a[1] = new HourlyEmployee("李四",3,20,200);
a[2] = new SalesEmployee("王五",4,50000,(float)0.1);
a[3] = new BasePlusSalesEmployee("刘六",5,50000,(float)0.1,1000);
System.out.println(String.format("固定工资员工:张三的工资为:%.2f",a[0].getSalary(2)));
System.out.println(String.format("小时工员工:李四的工资为:%.2f",a[1].getSalary(2)));
System.out.println(String.format("销售员员工:王五的工资为:%.2f",a[2].getSalary(2)));
System.out.println(String.format("固定底薪销售员:刘六的工资为:%.2f",a[3].getSalary(2)));
}
}
因为老师要求要用setter和getter方法,所以我就没删除,如果大家不需要,可以自己手动删除一下。
运行结果: