深圳幻海软件技术有限公司 欢迎您!

JAVA面向对象练习题,课后编程题。题目为:公司员工分为5类,每类员工都有相应的封装类。

2023-06-07

abstractclassEmployee{privateintmonth;privateStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetMo
  1. abstract class Employee{
  2. private int month;
  3. private String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getMonth() {
  11. return month;
  12. }
  13. public void setMonth(int month) {
  14. this.month = month;
  15. }
  16. public double getSalary(int month){
  17. int salary = 0;
  18. if(month==this.month)
  19. salary=salary+100;
  20. return salary;
  21. }
  22. public Employee(){
  23. }
  24. public Employee(String name,int month){
  25. this.month=month;
  26. this.name=name;
  27. }
  28. }
  29. class SalariedEmployee extends Employee{
  30. private int monthSalary;
  31. public SalariedEmployee(){
  32. }
  33. public SalariedEmployee(String name,int month,int monthSalary){
  34. super(name,month);
  35. this.monthSalary=monthSalary;
  36. }
  37. public int getMonthSalary(){
  38. return monthSalary;
  39. }
  40. public void setMonthSalary(int monthSalary){
  41. this.monthSalary = monthSalary;
  42. }
  43. public double getSalary(int month){
  44. double salary = monthSalary+super.getSalary(month);
  45. return salary;
  46. }
  47. }
  48. class HourlyEmployee extends Employee{
  49. public double getHourlySalary() {
  50. return hourlySalary;
  51. }
  52. public void setHourlySalary(double hourlySalary) {
  53. this.hourlySalary = hourlySalary;
  54. }
  55. public int getHours() {
  56. return hours;
  57. }
  58. public void setHours(int hours) {
  59. this.hours = hours;
  60. }
  61. private double hourlySalary;
  62. private int hours;
  63. public HourlyEmployee(){
  64. }
  65. public HourlyEmployee(String name,int month,double hourlySalary,int hours){
  66. super(name,month);
  67. this.hourlySalary=hourlySalary;
  68. this.hours=hours;
  69. }
  70. public double getSalary(int month){
  71. if(hours<0){
  72. System.out.println("数据错误!!!!!");
  73. return 0;
  74. }
  75. else if(hours<=160)
  76. return hourlySalary*hours+super.getSalary(month);
  77. else return hourlySalary*160+hourlySalary*1.5*(hours-160)+super.getSalary(month);
  78. }
  79. }
  80. class SalesEmployee extends Employee{
  81. private double sales;
  82. private double rete;
  83. public double getSales() {
  84. return sales;
  85. }
  86. public void setSales(double sales) {
  87. this.sales = sales;
  88. }
  89. public double getRete() {
  90. return rete;
  91. }
  92. public void setRete(double rete) {
  93. this.rete = rete;
  94. }
  95. public SalesEmployee(){
  96. }
  97. public SalesEmployee(String name,int month,double sales,double rete){
  98. super(name,month);
  99. this.sales=sales;
  100. this.rete=rete;
  101. }
  102. @Override
  103. public double getSalary(int month) {
  104. return this.getSales()*(1+this.getRete())+super.getSalary(month);
  105. }
  106. }
  107. class BasePlusSalesEployee extends SalesEmployee{
  108. public double getBaseSalary() {
  109. return baseSalary;
  110. }
  111. public void setBaseSalary(double baseSalary) {
  112. this.baseSalary = baseSalary;
  113. }
  114. private double baseSalary;
  115. public BasePlusSalesEployee(){
  116. }
  117. public BasePlusSalesEployee(String name,int month,double sales,double rete,double baseSalary){
  118. super(name,month,sales,rete);
  119. this.baseSalary=baseSalary;
  120. }
  121. public double getSalary(int month){
  122. return baseSalary+super.getSalary(month);
  123. }
  124. }
  125. public class 一 {
  126. public static void main(String[] args) {
  127. Employee[] employees = {new SalariedEmployee("张三",1,1600),new HourlyEmployee("李 四",2,50,180),new SalesEmployee("王五",3,6500,0.15),
  128. new BasePlusSalesEployee("赵六",4,5000,0.15,2000)};
  129. for(int i=0;i<employees.length;i++){
  130. System.out.println(Math.round(employees[i].getSalary(5)));
  131. }
  132. }
  133. }

某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下
(1)Employee:这是所有员工的父类。
①属性:员工的姓名、员工的生日月份。
)方法:getSalary(int month)根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。
(2)SalariedEmployee:Employee 的子类,拿固定工资的员工。
属性:月薪。
(3)HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的
部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数。
(4)SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率。
(5)BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为
底薪加上销售提成。
属性:底薪。
本题要求根据上述员工分类,编写一个程序,实现以下功能:
(1)创建一个Employee数组,分别创建若干不同的Employee对象,并打印某个月的
(2)每个类都完全封装,不允许有非私有化属性。

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览47725 人正在系统学习中