eclipse定义一个Employee抽象类,里面包含work()抽象方法,子类是manager和cook;成员属性有ID,name,salary.不用private
时间: 2024-03-15 13:44:17 浏览: 15
好的,这是一个在 Eclipse 中定义 Employee 抽象类的示例代码,包含了 work() 抽象方法和成员属性 ID、name、salary,还有 Manager 和 Cook 两个子类。这里不使用 private 修饰符,将属性设置为了默认访问修饰符。
```java
public abstract class Employee {
int ID;
String name;
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 {
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.name + " is managing " + this.department + ".");
}
}
public class Cook extends Employee {
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.name + " is cooking " + this.cuisine + " dishes.");
}
}
```
在这个示例代码中,Employee 是一个抽象类,包含了 ID、name、salary 三个成员属性和一个抽象方法 work(),而 Manager 和 Cook 是它的两个子类,它们都实现了 work() 方法,并添加了额外的成员属性 department 和 cuisine。注意,这里没有使用任何访问修饰符,表示这些属性可以被同一个包中的其他类访问。