某公司的雇员分为以下若干类: Employee:这是所有员工总的父类, 属性: 员工的姓名,员工的生日月份。 方法:getSalary(
生活随笔
收集整理的這篇文章主要介紹了
某公司的雇员分为以下若干类: Employee:这是所有员工总的父类, 属性: 员工的姓名,员工的生日月份。 方法:getSalary(
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼
/*某公司的雇員分為以下若干類:Employee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(intmonth)根據(jù)參數(shù)月份來確定工資,如果該月員工過生日,則公司會(huì)額外獎(jiǎng)勵(lì)100 元。SalariedEmployee:Employee 的子類,拿固定工資的員工。屬性:月薪HourlyEmployee:Employee 的子類, 按小時(shí)拿工資的員工,每月工作超出160 小時(shí)的部分按照1.5 倍工資發(fā)放。屬性:每小時(shí)的工資、每月工作的小時(shí)數(shù)SalesEmployee:Employee 的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率BasePlusSalesEmployee:SalesEmployee 的子類,有固定底薪的銷售人員,工資 由底薪加上銷售提成部分。屬性:底薪。根據(jù)要求創(chuàng)建 SalariedEmployee 、 HourlyEmployees 、SaleEmployee 和 BasePlusSalesEmployee四個(gè)類的對象各一個(gè),并計(jì)算某個(gè)月這四個(gè)對象的工資。注意:要求把每個(gè)類都做成完全封裝,不允許非私有化屬性。*/ public class Homework1 {public static void main(String[] args) {//創(chuàng)建對象Employee e1 = new SalariedEmployee("張三", 1, 2500);Employee e2 = new HourlyEmployee("李四", 2, 100, 200);Employee e3 = new SalesEmployee("趙六", 3, 1000000, 0.01);Employee e4 = new BasePlusSalesEmployee("錢七",4,100000, 0.02, 500);//獲得薪水System.out.println(e1.getName() + "的工資是:" + e1.getSalary(4));System.out.println(e2.getName() + "的工資是:" + e2.getSalary(4));System.out.println(e3.getName() + "的工資是:" + e3.getSalary(4));System.out.println(e4.getName() + "的工資是:" + e4.getSalary(4));} }/* Employee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(intmonth)根據(jù)參數(shù)月份來確定工資,如果該月員工過生日,則公司會(huì)額外獎(jiǎng)勵(lì)100 元。 */ abstract class Employee{private String name;private int month;//constructorpublic Employee() {}public Employee(String name, int month) {this.name = name;this.month = month;}//setter and getterpublic 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 abstract double getSalary(int month); }/* SalariedEmployee:Employee 的子類,拿固定工資的員工。屬性:月薪 */ class SalariedEmployee extends Employee{//月薪private int monthlySalary;@Overridepublic double getSalary(int month) {if (this.getMonth() == month){return this.getMonthlySalary() + 100;}else{return this.getMonthlySalary();}}//constructorpublic SalariedEmployee() {}public SalariedEmployee(String name, int month, int monthlySalary) {super(name, month);this.monthlySalary = monthlySalary;}//setter and getterpublic int getMonthlySalary() {return monthlySalary;}public void setMonthlySalary(int monthlySalary) {this.monthlySalary = monthlySalary;} }/*HourlyEmployee:Employee 的子類, 按小時(shí)拿工資的員工,每月工作超出160 小時(shí)的部分按照1.5 倍工資發(fā)放。屬性:每小時(shí)的工資、每月工作的小時(shí)數(shù) */ class HourlyEmployee extends Employee{//小時(shí)工資int hourlyPayment;//每月工作小時(shí)數(shù)int workHour;@Overridepublic double getSalary(int month) {if (this.getWorkHour() > 160){if (this.getMonth() == month){return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5 + 100;}return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5;}else{if (this.getMonth() == month){return getWorkHour()*getHourlyPayment() + 100;}return getHourlyPayment()*getWorkHour();}}//constructorpublic HourlyEmployee() {}public HourlyEmployee(String name, int month, int hourlyPayment, int workHour) {super(name, month);this.hourlyPayment = hourlyPayment;this.workHour = workHour;}//setter and getterpublic int getHourlyPayment() {return hourlyPayment;}public void setHourlyPayment(int hourlyPayment) {this.hourlyPayment = hourlyPayment;}public int getWorkHour() {return workHour;}public void setWorkHour(int workHour) {this.workHour = workHour;} }/* SalesEmployee:Employee 的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率 */ class SalesEmployee extends Employee{private double monthlySale;//提成率應(yīng)在0~1之間private double commissionRate;//constructorpublic SalesEmployee() {}public SalesEmployee(String name, int month, double monthlySale, double commissionRate) {super(name, month);this.monthlySale = monthlySale;this.commissionRate = commissionRate;}//setter and getterpublic double getMonthlySale() {return monthlySale;}public void setMonthlySale(double monthlySale) {this.monthlySale = monthlySale;}public double getCommissionRate() {return commissionRate;}public void setCommissionRate(double commissionRate) {this.commissionRate = commissionRate;}@Overridepublic double getSalary(int month) {if (this.getMonth() == month){return this.getMonthlySale()*this.getCommissionRate() + 100;}else{return this.getMonthlySale()*this.getCommissionRate();}} }/* BasePlusSalesEmployee:SalesEmployee 的子類,有固定底薪的銷售人員,工資 由底薪加上銷售提成部分。屬性:底薪。 */ class BasePlusSalesEmployee extends SalesEmployee{private double baseSalary;//constructorpublic BasePlusSalesEmployee() {}public BasePlusSalesEmployee(String name, int month, double monthlySale, double commissionRate, double baseSalary) {super(name, month, monthlySale, commissionRate);this.baseSalary = baseSalary;}//setter and getterpublic double getBaseSalary() {return baseSalary;}public void setBaseSalary(double baseSalary) {this.baseSalary = baseSalary;}//重寫父類獲得薪水的方法@Overridepublic double getSalary(int month) {if (this.getMonth() == month){return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate() + 100;}else{return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate();}} }總結(jié)
以上是生活随笔為你收集整理的某公司的雇员分为以下若干类: Employee:这是所有员工总的父类, 属性: 员工的姓名,员工的生日月份。 方法:getSalary(的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python之禅中文原文_Python之
- 下一篇: OJ1158: 又是排序(指针专题)(C