定义一个Employee抽象类,里面包含work()抽象方法,子类是manager和cook;成员属性有ID,name,salary.
时间: 2024-03-15 21:44:10 浏览: 77
好的,这是一个定义Employee抽象类的示例代码,包含了work()抽象方法和成员属性ID、name、salary,还有Manager和Cook两个子类:
```python
from abc import ABC, abstractmethod
class Employee(ABC):
def __init__(self, ID, name, salary):
self.ID = ID
self.name = name
self.salary = salary
@abstractmethod
def work(self):
pass
class Manager(Employee):
def __init__(self, ID, name, salary, department):
super().__init__(ID, name, salary)
self.department = department
def work(self):
print(f"{self.name} is managing {self.department}.")
class Cook(Employee):
def __init__(self, ID, name, salary, cuisine):
super().__init__(ID, name, salary)
self.cuisine = cuisine
def work(self):
print(f"{self.name} is cooking {self.cuisine} dishes.")
```
在这个示例代码中,Employee是一个抽象类,包含了ID、name、salary三个成员属性和一个抽象方法work(),而Manager和Cook是它的两个子类,它们都实现了work()方法,并添加了额外的成员属性department和cuisine。
阅读全文