Home > Back-end >  New to Java, could you tell me how to do bosses this problem
New to Java, could you tell me how to do bosses this problem

Time:10-04

Define an abstract class Employee, which contains abstract methods earning ();
Definition of Employee three subclasses YearWorker MonthWorker and WeekWorker, rewrite the earning methods in subclasses, the output of each type of workers wages,
Define Company class, in which the main method, define the length of the array for the three types of Employee, three elements respectively YearWorker, MonthWorker and WeekWorker object, then calls the earning methods, output each person pay,

CodePudding user response:

 
/* *
* @ author: heiye
* @ date: 2020-03-26 09:33
* */
Public class Company {

Public static void main (String [] args) {
The Employee [] employees={new YearWorker (120000), the new MonthWorker (10000), the new WeekWorker (2500)};
For (the Employee the Employee: employees) {
The employee. Earning ();
}
}
}

The abstract class Employee {
Public double salary;
The abstract void earning ();
}

The class YearWorker extends the Employee {

Public YearWorker (double salaryParam) {
This. Salary=salaryParam;
}

@ Override
Void earning () {
System. The out. Println (" YearWorker: "+ this. Salary).
}
}
The class MonthWorker extends the Employee {

Public MonthWorker (double salaryParam) {
This. Salary=salaryParam;
}

@ Override
Void earning () {
System. The out. Println (" MonthWorker: "+ this. Salary).
}
}
The class WeekWorker extends the Employee {

Public WeekWorker (double salaryParam) {
This. Salary=salaryParam;
}

@ Override
Void earning () {
System. The out. Println (" WeekWorker: "+ this. Salary).
}
}
  • Related