编写一个学生和教师的数据输入和显示程序,学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号姓名、性别、年龄的输入和显示设计成一个类persona,并作为学生类student和教师类teacher的基类
时间: 2023-12-22 21:21:14 浏览: 190
C++小型学生成绩管理系统.docx
下面是一个示例代码,实现了学生和教师的数据输入和显示,其中使用了一个名为persona的基类来存储共同的属性。请注意,这只是一个示例代码,可能需要根据实际需求进行修改和优化。
```python
class persona:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
def display(self):
print("ID:", self.id)
print("Name:", self.name)
print("Gender:", self.gender)
print("Age:", self.age)
class student(persona):
def __init__(self, id, name, gender, age, department, score):
super().__init__(id, name, gender, age)
self.department = department
self.score = score
def display(self):
super().display()
print("Department:", self.department)
print("Score:", self.score)
class teacher(persona):
def __init__(self, id, name, gender, age, title, department):
super().__init__(id, name, gender, age)
self.title = title
self.department = department
def display(self):
super().display()
print("Title:", self.title)
print("Department:", self.department)
# 示例用法
s = student(1, "John", "Male", 20, "Computer Science", 90)
s.display()
t = teacher(2, "Mary", "Female", 35, "Professor", "Computer Science")
t.display()
```
输出结果:
```
ID: 1
Name: John
Gender: Male
Age: 20
Department: Computer Science
Score: 90
ID: 2
Name: Mary
Gender: Female
Age: 35
Title: Professor
Department: Computer Science
```
阅读全文