定义员工类,具有姓名、年龄、州别属性,并具有构造方法和显示数据方法, 定义管理层类,继承员工类,并分自己的属性职务和年薪。 定义职员类,继承员工类,并有自己的属性所属部门和月薪。 通过测试类显示各层人员信息。
时间: 2024-05-02 15:16:44 浏览: 53
python15 类的继承
以下是实现该功能的Java代码:
```
//员工类
class Employee {
String name;
int age;
String state;
//构造方法
public Employee(String name, int age, String state) {
this.name = name;
this.age = age;
this.state = state;
}
//显示数据方法
public void display() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("州别:" + state);
}
}
//管理层类
class Management extends Employee {
String position;
int salary;
//构造方法
public Management(String name, int age, String state, String position, int salary) {
super(name, age, state);
this.position = position;
this.salary = salary;
}
//显示数据方法
public void display() {
super.display();
System.out.println("职务:" + position);
System.out.println("年薪:" + salary);
}
}
//职员类
class Staff extends Employee {
String department;
int monthlySalary;
//构造方法
public Staff(String name, int age, String state, String department, int monthlySalary) {
super(name, age, state);
this.department = department;
this.monthlySalary = monthlySalary;
}
//显示数据方法
public void display() {
super.display();
System.out.println("所属部门:" + department);
System.out.println("月薪:" + monthlySalary);
}
}
//测试类
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("Tom", 25, "New York");
Management m1 = new Management("John", 35, "California", "CEO", 1000000);
Staff s1 = new Staff("Mary", 30, "Texas", "Finance", 5000);
e1.display();
System.out.println();
m1.display();
System.out.println();
s1.display();
}
}
```
运行结果:
```
姓名:Tom
年龄:25
州别:New York
姓名:John
年龄:35
州别:California
职务:CEO
年薪:1000000
姓名:Mary
年龄:30
州别:Texas
所属部门:Finance
月薪:5000
```
阅读全文