java实现 编程完整实现 (教材P118 四、编程题) 定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,并有自己的属性:职务和年薪。定义职员类,继承员工类,并有自己的属性:所属部门和月薪。
时间: 2024-05-06 20:18:10 浏览: 75
以下是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 display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}
```
管理层类:
```
public class Manager extends Employee {
private String position;
private double salary;
public Manager(String name, int age, String gender, String position, double salary) {
super(name, age, gender);
this.position = position;
this.salary = salary;
}
public void display() {
super.display();
System.out.println("Position: " + position);
System.out.println("Salary: " + salary);
}
}
```
职员类:
```
public class Staff extends Employee {
private String department;
private double monthlySalary;
public Staff(String name, int age, String gender, String department, double monthlySalary) {
super(name, age, gender);
this.department = department;
this.monthlySalary = monthlySalary;
}
public void display() {
super.display();
System.out.println("Department: " + department);
System.out.println("Monthly Salary: " + monthlySalary);
}
}
```
以上就是完整的Java实现代码。您可以通过创建对象并调用相应的方法来使用这些类。例如:
```
Employee employee = new Employee("John", 30, "Male");
employee.display();
Manager manager = new Manager("Jane", 40, "Female", "CEO", 100000);
manager.display();
Staff staff = new Staff("Tom", 25, "Male", "HR", 5000);
staff.display();
```
阅读全文