编写一个学生和教师相关数据输入和显示程序。设计三个类 person, student 和 teacher 类,其中 student 和 teacher 是由 person 类派生出来的。学生(student)数据有 学号、姓名、性别、年龄、班级号和期末三门功课总分;教师(teacher)数据有职工号、 姓名、性别、年龄、职称和系别。
时间: 2023-12-19 14:07:18 浏览: 72
好的,这是一个比较典型的面向对象编程问题,我来为您提供一下代码实现。
```python
class Person:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
class Student(Person):
def __init__(self, id, name, gender, age, class_num, score):
super().__init__(id, name, gender, age)
self.class_num = class_num
self.score = score
class Teacher(Person):
def __init__(self, id, name, gender, age, job_title, department):
super().__init__(id, name, gender, age)
self.job_title = job_title
self.department = department
def input_student():
id = input('请输入学号:')
name = input('请输入姓名:')
gender = input('请输入性别:')
age = input('请输入年龄:')
class_num = input('请输入班级号:')
score = input('请输入期末三门功课总分:')
return Student(id, name, gender, age, class_num, score)
def input_teacher():
id = input('请输入职工号:')
name = input('请输入姓名:')
gender = input('请输入性别:')
age = input('请输入年龄:')
job_title = input('请输入职称:')
department = input('请输入系别:')
return Teacher(id, name, gender, age, job_title, department)
def show_student(student):
print('学号:', student.id)
print('姓名:', student.name)
print('性别:', student.gender)
print('年龄:', student.age)
print('班级号:', student.class_num)
print('期末三门功课总分:', student.score)
def show_teacher(teacher):
print('职工号:', teacher.id)
print('姓名:', teacher.name)
print('性别:', teacher.gender)
print('年龄:', teacher.age)
print('职称:', teacher.job_title)
print('系别:', teacher.department)
# 测试代码
s = input_student()
t = input_teacher()
show_student(s)
show_teacher(t)
```
运行上述代码后,您可以依次输入学生和教师的信息,程序将会自动将输入的数据保存到对应的类实例中,并且可以通过调用 `show_student` 和 `show_teacher` 方法,将实例的数据打印出来。
阅读全文