1 /*某公司的雇员分为以下若干类: 2 Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。 3 SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪 4 HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数 5 SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 6 BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。 7 写一个程序,把若干各种类型的员工放在一个Employee数组里,写一个方法,打印出某月每个员工的工资数额。注意:要求把每个类都做成完全封装,不允许非私有化属性。 8 */ 9 10 11 12 //员工类 13 public class Employee 14 { 15 private String name; //员工的姓名 16 private int month; //员工的生日月份 17 18 public Employee(String name,int month) 19 { 20 this.setName(name); 21 this.setMonth(month); 22 } 23 24 public double getSalary(int month) 25 { 26 //如果月份为员工生日,则增加100 27 return (month==this.month)?100:0; 28 } 29 30 public String getName() 31 { 32 return name; 33 } 34 35 public void setName(String name) 36 { 37 this.name = name; 38 } 39 40 public int getMonth() 41 { 42 return month; 43 } 44 45 public void setMonth(int month) 46 { 47 this.month = month; 48 } 49 50 51 } 52 53 54 //拿固定工资的员工 55 public class SalariedEmployee extends Employee 56 { 57 private double monthPay; //月薪 58 59 public SalariedEmployee(String name,int month,double monthPay) 60 { 61 super(name,month); 62 this.setMonthPay(monthPay); 63 } 64 65 public double getSalary(int month) 66 { 67 return this.getMonthPay() + super.getSalary(month); 68 } 69 70 public double getMonthPay() 71 { 72 return monthPay; 73 } 74 75 public void setMonthPay(double monthPay) 76 { 77 this.monthPay = monthPay; 78 } 79 } 80 81 82 //按小时拿工资的员工 83 public class HourlyEmployee extends Employee 84 { 85 private int hour; //工作的小时数 86 private double hourPay; //每小时的工资 87 88 public HourlyEmployee(String name,int month,int hour,int hourPay) 89 { 90 super(name,month); 91 this.setHour(hour); 92 this.setHourPay(hourPay); 93 } 94 95 public double getSalary(int month) 96 { 97 if(hour>16) 98 { 99 return 160*(this.getHourPay()) + (this.getHourPay())*1.5*(hour-160)+super.getSalary(month); 100 } 101 else 102 { 103 return hour*this.getHourPay()+super.getSalary(month); 104 } 105 } 106 107 public int getHour() 108 { 109 return hour; 110 } 111 112 public void setHour(int hour) 113 { 114 this.hour = hour; 115 } 116 117 public double getHourPay() 118 { 119 return hourPay; 120 } 121 122 public void setHourPay(double hourPay) 123 { 124 this.hourPay = hourPay; 125 } 126 127 128 } 129 130 //销售人员 131 public class SalesEmployee extends Employee 132 { 133 private int monthSales; //销售额 134 private double rate; //提出率 135 136 public SalesEmployee(String name,int month,int monthSales,double rate) 137 { 138 super(name,month); 139 this.setMonthSales(monthSales); 140 this.setRate(rate); 141 } 142 143 public double getSalary(int month) 144 { 145 return this.getRate()*this.getMonthSales() + super.getSalary(month); 146 } 147 148 public int getMonthSales() 149 { 150 return monthSales; 151 } 152 153 public void setMonthSales(int monthSales) 154 { 155 this.monthSales = monthSales; 156 } 157 158 public double getRate() 159 { 160 return rate; 161 } 162 163 public void setRate(double rate) 164 { 165 this.rate = rate; 166 } 167 168 169 } 170 171 172 //有固定底薪的销售人员 173 public class BasePlusSalesEmployee extends SalesEmployee 174 { 175 public double basePay; 176 177 public BasePlusSalesEmployee(String name,int month,int monthSales,double rate,double basePay) 178 { 179 super(name,month,monthSales,rate); 180 this.setBasePay(basePay); 181 } 182 183 public double getSalary(int month) 184 { 185 return super.getSalary(month)+basePay; 186 } 187 188 public double getBasePay() 189 { 190 return basePay; 191 } 192 193 public void setBasePay(double basePay) 194 { 195 this.basePay = basePay; 196 } 197 198 199 } 200 201 202 public class Test 203 { 204 public static void main(String args[]) 205 { 206 Employee a[] = new Employee[4]; 207 208 a[0] = new SalariedEmployee("张三", 2, 1000); 209 a[1] = new HourlyEmployee("李四", 3, 200, 2000); 210 a[2] = new SalesEmployee("王五", 4, 50000, (float) 0.1); 211 a[3] = new BasePlusSalesEmployee("小明", 5, 50000, (float) 0.1, 1000); 212 213 System.out.println("张三的工资为" + a[0].getSalary(2)); 214 System.out.println("李四的工资为" + a[1].getSalary(2)); 215 System.out.println("王五的工资为" + a[2].getSalary(2)); 216 System.out.println("小明的工资为" + a[3].getSalary(2)); 217 218 } 219 }
张三的工资为1100.0
李四的工资为440000.0
王五的工资为5000.000074505806
小明的工资为6000.000074505806