java员工与经理抽象类
时间: 2023-12-06 12:33:47 浏览: 83
java抽象类
好的,我理解了你的问题。针对这个问题,我可以给你提供一个抽象类的示例代码:
```
public abstract class Employee {
private String name;
private int age;
private String gender;
private String address;
private String phoneNumber;
private String email;
public Employee(String name, int age, String gender, String address, String phoneNumber, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
public abstract double calculateSalary();
// getters and setters
}
public abstract class Manager extends Employee {
private String department;
public Manager(String name, int age, String gender, String address, String phoneNumber, String email, String department) {
super(name, age, gender, address, phoneNumber, email);
this.department = department;
}
public abstract void manage();
// getters and setters
}
```
在这个示例代码中,我们定义了两个抽象类:`Employee` 和 `Manager`。`Employee` 类包含了一些基本的员工信息,如姓名、年龄、性别、地址、电话号码和电子邮件地址,并且定义了一个抽象方法 `calculateSalary()`,用于计算员工的薪水。`Manager` 类继承自 `Employee` 类,并且添加了一个部门属性和一个抽象方法 `manage()`,用于管理部门。
当我们需要创建具体的员工和经理时,可以继承这两个抽象类,并实现它们的抽象方法。例如:
```
public class Programmer extends Employee {
private double salary;
public Programmer(String name, int age, String gender, String address, String phoneNumber, String email, double salary) {
super(name, age, gender, address, phoneNumber, email);
this.salary = salary;
}
@Override
public double calculateSalary() {
return salary;
}
// getters and setters
}
public class SalesManager extends Manager {
private double bonus;
public SalesManager(String name, int age, String gender, String address, String phoneNumber, String email, String department, double bonus) {
super(name, age, gender, address, phoneNumber, email, department);
this.bonus = bonus;
}
@Override
public void manage() {
System.out.println("I am managing the sales department.");
}
// getters and setters
}
```
在这个示例代码中,我们创建了一个具体的 `Programmer` 类和一个具体的 `SalesManager` 类,它们分别继承自 `Employee` 和 `Manager` 类,并实现了它们的抽象方法。
阅读全文