分别定义Person类、Employee类、Student类以及在职研究生类PartTime Graduate Student,并且画出相应的继承层次图。
时间: 2024-02-01 15:13:43 浏览: 151
以下是定义的代码和继承层次图:
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
class Employee(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.salary = salary
def work(self):
print(f"{self.name} is working hard.")
class Student(Person):
def __init__(self, name, age, school):
super().__init__(name, age)
self.school = school
def study(self):
print(f"{self.name} is studying at {self.school}.")
class PartTimeGraduateStudent(Student):
def __init__(self, name, age, school, company):
super().__init__(name, age, school)
self.company = company
def work_and_study(self):
print(f"{self.name} is working at {self.company} and studying at {self.school}.")
```
继承层次图:
```
+--------+
| Person |
+--------+
|
+------+------+
| |
+---------+ +---------+
| Employee| | Student |
+---------+ +---------+
|
+---------------+
| PartTimeGraduate|
+---------------+
```
阅读全文