eclipse定义一个Employee抽象类,里面包含work()抽象方法,子类是manager和cook;成员属性有ID,name,salary.
时间: 2024-03-15 16:44:11 浏览: 24
好的,这是一个在 Eclipse 中定义 Employee 抽象类的示例代码,包含了 work() 抽象方法和成员属性 ID、name、salary,还有 Manager 和 Cook 两个子类:
```java
public abstract class Employee {
private int ID;
private String name;
private double salary;
public Employee(int ID, String name, double salary) {
this.ID = ID;
this.name = name;
this.salary = salary;
}
public abstract void work();
}
public class Manager extends Employee {
private String department;
public Manager(int ID, String name, double salary, String department) {
super(ID, name, salary);
this.department = department;
}
@Override
public void work() {
System.out.println(this.getName() + " is managing " + this.department + ".");
}
}
public class Cook extends Employee {
private String cuisine;
public Cook(int ID, String name, double salary, String cuisine) {
super(ID, name, salary);
this.cuisine = cuisine;
}
@Override
public void work() {
System.out.println(this.getName() + " is cooking " + this.cuisine + " dishes.");
}
}
```
在这个示例代码中,Employee 是一个抽象类,包含了 ID、name、salary 三个成员属性和一个抽象方法 work(),而 Manager 和 Cook 是它的两个子类,它们都实现了 work() 方法,并添加了额外的成员属性 department 和 cuisine。注意,这里使用了 @Override 注解来覆盖父类的抽象方法。