java 用继承完成①学校有2种员工:助教(Assistant)、收银员(Cashier)。助教的行为action()是:协助老师修改作业;收银员的行为action()是:在餐馆打工收银。②员工可以
以下是Java代码实现:
```
//员工类
public class Employee {
public void action() {
//员工的行为
}
}
//助教类,继承自员工类
public class Assistant extends Employee {
@Override
public void action() {
System.out.println("正在协助老师批作业...");
}
}
//收银员类,继承自员工类
public class Cashier extends Employee {
@Override
public void action() {
System.out.println("正在餐馆打工收银...");
}
}
//学校类
public class School {
public void enter(Employee employee) {
//员工进入学校
}
public void run() {
//学校运营
}
}
//测试类
public class Test {
public static void main(String[] args) {
School school = new School();
Assistant xiaodong = new Assistant();
Cashier xiaoming = new Cashier();
school.enter(xiaodong);
xiaodong.action();
school.enter(xiaoming);
xiaoming.action();
}
}
```
输出结果为:
```
正在协助老师批作业...
正在餐馆打工收银...
```
```
//员工类
public class Employee {
public void action() {
//员工的行为
}
}
//助教类,继承自员工类
public class Assistant extends Employee {
@Override
public void action() {
System.out.println("正在协助老师批作业...");
}
}
//收银员类,继承自员工类
public class Cashier extends Employee {
@Override
public void action() {
System.out.println("正在餐馆打工收银...");
}
}
//学校类
public class School {
public void enter(Employee employee) {
//员工进入学校
}
public void run() {
//学校运营
}
}
//测试类
public class Test {
public static void main(String[] args) {
School school = new School();
Assistant xiaodong = new Assistant();
Cashier xiaoming = new Cashier();
school.enter(xiaodong);
xiaodong.action();
school.enter(xiaoming);
xiaoming.action();
}
}
```
输出结果为:
```
正在协助老师批作业...
正在餐馆打工收银...
```