## ✌✌✌The ancients have a lot of words, good memory is not as good as bad pens, a journey of a thousand miles begins with a single step, a thousand lines of code every day is essential, a daily summary and writing, a target factory, full of hope, the club will be invincible, ha Haha! ! ! ✌✌✌
1. ✌Question requirements
Implement an abstract class named Employee, Employee has two subclasses Manager and Worker.
Specific requirements are as follows:
- The attributes in the Employee class are name (String), department (String), phone number telephone (String), monthly salary month_salary (int), annual salary year_salary (int)
- The attributes in the Manager class are: level (int)
- The attributes in the Staff class are: entry year (int)
- Methods in the Employee class:
a. Constructor with parameters
Employee(String name,String department,String telephone, int month_salary)
b. Abstract method calYearSalary() - Methods in the Manager class:
a. Constructor with parameters (requires calling the constructor of the parent class)
Manager(String name,String department,String telephone,int month_salary,int level)
b. The upgrade method
adds 1 to the current level value
c. Write the abstract method of the parent class
calYearSalary (the formula is annual salary = monthly salary * (12+level))
d. toString method (output employee information) - Methods in the Staff class:
a. Constructor with parameters (requires calling the constructor of the parent class)
Staff(String name, String department, String telephone, int month_salary, int year)
b. Override the abstract method of the parent class
calYearSalary (the formula is Annual salary = monthly salary 12 + entry period 1000)
c. toString method (output employee information)
Write a java program to implement these 3 classes, create an Employee array with a size of 4 in the main method, instantiate the array elements with subscripts 0 and 1 as Manager objects, and instantiate the array elements with subscripts 2, 3 as Staff Object, use a loop to call the toString method of each array element to output employee information.
Two, ✌code implementation
1.✌Main class
/*
项目名:上机1
文件名:题目2
作者:魏宝航
创作时间:2020.03.20 22:30
*/
public class Main {
public static void main(String[] args) {
//1、创建对象数组
Employee[] array=new Employee[4];
//2、初始化数组
array[0]=new Manager("刘小刚","经理","人事","13892812930",6000,3);
array[1]=new Manager("赵爱玲","秘书","采购","13958240972",12000,5);
array[2]=new Staff("李佳薇","普通员工","财务","13688729082",3000,3);
array[3]=new Staff("张美丽","总监","研发","13948602379",7000,6);
//3、打印结果
for(int i=0;i<4;i++){
System.out.println("员工:"+(i+1));
System.out.println(array[i].toString());
System.out.println("---------------------------");
}
}
}
2.✌Employee abstract class
abstract class Employee {
//共有字段
public String name;
public String post;
public String department;
public String telephone;
public int month_salary;
public int year_salary;
//空参构造
public Employee() {
}
//有参构造
public Employee(String name, String post,String department, String telephone, int month_salary) {
this.name = name;
this.post=post;
this.department = department;
this.telephone = telephone;
this.month_salary = month_salary;
}
//计算年薪方法
public abstract void calYearSalary();
}
3.✌Manager class
class Manager extends Employee {
//静态代码块
{
calYearSalary();
}
//级别
public int level;
//空参构造
public Manager(){
}
//有参构造
public Manager(String name, String post,String department, String telephone, int month_salary, int level) {
super(name, post,department, telephone, month_salary);
this.level = level;
}
//级别计算方法
public void upgrade() {
this.level += 1;
}
//计算年薪
@Override
public void calYearSalary() {
this.year_salary = month_salary * (12 + this.level);
}
//重写toString方法
@Override
public String toString() {
return "姓名:" + name + "职务:"+post+"\n部门:" + department + "\n手机号码:" + telephone + "\n月薪:" + month_salary + "\n年薪:" + year_salary + "\n级别:" + level;
}
}
4.✌Staff category
class Staff extends Employee {
//静态代码块
{
calYearSalary();
}
//入职年限
public int year;
//空参构造
public Staff(){
}
//有参构造
public Staff(String name, String post,String department, String telephone, int month_salary, int year) {
super(name, post, department, telephone, month_salary);
this.year = year;
}
//计算年薪
@Override
public void calYearSalary() {
this.year_salary = month_salary * 12 + this.year * 1000;
}
//重写同String方法
@Override
public String toString() {
return "姓名:" + name + "职务:"+post+"\n部门:" + department + "\n手机号码:" + telephone + "\n月薪:" + month_salary + "\n年薪:" + year_salary + "\n入职年限:" + year;
}
}
、