编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门,要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teach
时间: 2023-06-11 10:08:05 浏览: 188
下面是一个可能的实现,使用了面向对象的思想,分别定义了Person、Student和Teacher三个类,Person类用于存储编号和姓名,Student类和Teacher类分别继承Person类,并添加班号、成绩和职称、部门等属性。
```python
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
class Student(Person):
def __init__(self, id, name, class_no, score):
super().__init__(id, name)
self.class_no = class_no
self.score = score
class Teacher(Person):
def __init__(self, id, name, title, department):
super().__init__(id, name)
self.title = title
self.department = department
class StudentData:
def __init__(self):
self.students = []
def add(self, student):
self.students.append(student)
def display(self):
for student in self.students:
print("编号:{},姓名:{},班号:{},成绩:{}".format(student.id, student.name, student.class_no, student.score))
class TeacherData:
def __init__(self):
self.teachers = []
def add(self, teacher):
self.teachers.append(teacher)
def display(self):
for teacher in self.teachers:
print("编号:{},姓名:{},职称:{},部门:{}".format(teacher.id, teacher.name, teacher.title, teacher.department))
```
使用时,可以定义一个StudentData对象和一个TeacherData对象,调用它们的add方法添加数据,调用display方法显示数据。
```python
students = StudentData()
students.add(Student(1, "张三", "101", 90))
students.add(Student(2, "李四", "102", 85))
students.display()
teachers = TeacherData()
teachers.add(Teacher(1, "王老师", "教授", "计算机科学与技术"))
teachers.add(Teacher(2, "李老师", "副教授", "软件工程"))
teachers.display()
```
输出结果:
```
编号:1,姓名:张三,班号:101,成绩:90
编号:2,姓名:李四,班号:102,成绩:85
编号:1,姓名:王老师,职称:教授,部门:计算机科学与技术
编号:2,姓名:李老师,职称:副教授,部门:软件工程
```
阅读全文