编程实现类的继承。编写父类People,子类Employee继承自人类。人类具有姓名,性别,年龄等性质,还具有吃和说的行为。雇员类继承父类,还拥有工号性质和工作行为。构造人类和员工类的对象,调用吃、说、工作的方法输出有关信息。
时间: 2024-03-11 14:49:39 浏览: 85
好的,以下是实现上述功能的Python代码:
```python
class People:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def eat(self):
print(f"{self.name} is eating.")
def speak(self):
print(f"{self.name} is speaking.")
class Employee(People):
def __init__(self, name, gender, age, employee_id):
super().__init__(name, gender, age)
self.employee_id = employee_id
def work(self):
print(f"{self.name} is working with employee ID {self.employee_id}.")
# 创建People类对象
p1 = People("Tom", "Male", 25)
p1.eat() # Tom is eating.
p1.speak() # Tom is speaking.
# 创建Employee类对象
e1 = Employee("Jerry", "Male", 30, "001")
e1.eat() # Jerry is eating.
e1.speak() # Jerry is speaking.
e1.work() # Jerry is working with employee ID 001.
```
运行结果为:
```
Tom is eating.
Tom is speaking.
Jerry is eating.
Jerry is speaking.
Jerry is working with employee ID 001.
```
阅读全文