学生的屈性:姓名、性别、年龄、学号、年级、专业:学生的行为:吃饭、娱乐、学习、写作业、 教师的属性:姓名、性别、年龄、工号、职称、部门:教师的行为:吃饭、娱乐、授课、改作业。 请设计出对应的继承体系,并在主类中完成测试
时间: 2023-09-19 10:12:24 浏览: 58
以下是对应的继承体系设计:
```
class Person:
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 play(self):
print(f"{self.name} is playing.")
class Student(Person):
def __init__(self, name, gender, age, student_id, grade, major):
super().__init__(name, gender, age)
self.student_id = student_id
self.grade = grade
self.major = major
def study(self):
print(f"{self.name} is studying.")
def do_homework(self):
print(f"{self.name} is doing homework.")
class Teacher(Person):
def __init__(self, name, gender, age, teacher_id, title, department):
super().__init__(name, gender, age)
self.teacher_id = teacher_id
self.title = title
self.department = department
def teach(self):
print(f"{self.name} is teaching.")
def grade_homework(self):
print(f"{self.name} is grading homework.")
```
接下来我们进行测试:
```
student = Student("Alice", "female", 18, "20210001", "freshman", "Computer Science")
print(student.name) # Alice
print(student.student_id) # 20210001
student.eat() # Alice is eating.
student.study() # Alice is studying.
teacher = Teacher("Bob", "male", 35, "1001", "professor", "Computer Science")
print(teacher.name) # Bob
print(teacher.title) # professor
teacher.eat() # Bob is eating.
teacher.teach() # Bob is teaching.
```
输出结果如下:
```
Alice
20210001
Alice is eating.
Alice is studying.
Bob
professor
Bob is eating.
Bob is teaching.
```
阅读全文