定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,并有自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性所属部门和月薪。通过测试类显示各层人员信息。
时间: 2024-05-05 17:22:09 浏览: 187
定义一个人员类Person,数据成员有姓名,能够设置姓名的值及显示姓名
5星 · 资源好评率100%
以下是员工类、管理层类、职员类的代码实现:
```java
//员工类
public class Employee {
private String name;
private int age;
private String gender;
public Employee(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}
//管理层类
public class Management extends Employee {
private String position;
private double salary;
public Management(String name, int age, String gender, String position, double salary) {
super(name, age, gender);
this.position = position;
this.salary = salary;
}
public void displayInfo() {
super.displayInfo();
System.out.println("Position: " + position);
System.out.println("Salary: " + salary);
}
}
//职员类
public class Staff extends Employee {
private String department;
private double salary;
public Staff(String name, int age, String gender, String department, double salary) {
super(name, age, gender);
this.department = department;
this.salary = salary;
}
public void displayInfo() {
super.displayInfo();
System.out.println("Department: " + department);
System.out.println("Salary: " + salary);
}
}
```
以下是测试类的代码实现:
```java
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("Tom", 25, "Male");
e1.displayInfo();
System.out.println("----------------------");
Management m1 = new Management("Bob", 35, "Male", "CEO", 500000);
m1.displayInfo();
System.out.println("----------------------");
Staff s1 = new Staff("Alice", 30, "Female", "HR", 30000);
s1.displayInfo();
}
}
```
运行结果如下:
```
Name: Tom
Age: 25
Gender: Male
----------------------
Name: Bob
Age: 35
Gender: Male
Position: CEO
Salary: 500000.0
----------------------
Name: Alice
Age: 30
Gender: Female
Department: HR
Salary: 30000.0
```
阅读全文