编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门,要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
时间: 2023-06-12 09:03:52 浏览: 75
以下是一个简单的Python代码实现:
```python
class Person:
def __init__(self, num, name):
self.num = num
self.name = name
class Student(Person):
def __init__(self, num, name, class_num, score):
super().__init__(num, name)
self.class_num = class_num
self.score = score
class Teacher(Person):
def __init__(self, num, name, title, department):
super().__init__(num, name)
self.title = title
self.department = department
class Data:
students = []
teachers = []
def add_student(self, student):
self.students.append(student)
def add_teacher(self, teacher):
self.teachers.append(teacher)
def display_students(self):
for student in self.students:
print(f"编号:{student.num},姓名:{student.name},班号:{student.class_num},成绩:{student.score}")
def display_teachers(self):
for teacher in self.teachers:
print(f"编号:{teacher.num},姓名:{teacher.name},职称:{teacher.title},部门:{teacher.department}")
```
使用示例:
```python
data = Data()
# 添加学生数据
student1 = Student(1, "张三", "一班", 90)
student2 = Student(2, "李四", "二班", 85)
data.add_student(student1)
data.add_student(student2)
# 添加教师数据
teacher1 = Teacher(1, "王老师", "教授", "计算机科学与技术")
teacher2 = Teacher(2, "李老师", "副教授", "软件工程")
data.add_teacher(teacher1)
data.add_teacher(teacher2)
# 显示学生数据
data.display_students()
# 显示教师数据
data.display_teachers()
```
阅读全文