编写一个学生和教师的数据输入和显示程序。学生的数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并做为学生类Student和教师类Teacher的基类。
时间: 2023-07-10 14:17:49 浏览: 151
以下是一个Python的示例代码:
```python
class Person:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
def display(self):
print("编号:{},姓名:{},性别:{},年龄:{}".format(self.id, self.name, self.gender, self.age))
class Student(Person):
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("系别:{},成绩:{}".format(self.department, self.score))
class Teacher(Person):
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("职称:{},部门:{}".format(self.title, self.department))
# 示例代码
student = Student("001", "张三", "男", 20, "计算机科学与技术", 90)
teacher = Teacher("1001", "李四", "女", 40, "副教授", "计算机科学与技术")
student.display()
teacher.display()
```
在这个示例代码中,我们定义了一个基类Person,它包含了编号、姓名、性别和年龄的属性和一个display方法。这个类被学生类Student和教师类Teacher继承。
Student类和Teacher类都有自己的属性和display方法,用于显示学生和教师的信息。它们还通过super()调用了基类Person的display方法,来显示编号、姓名、性别和年龄的信息。
最后,我们创建了一个学生对象和一个教师对象,并调用它们的display方法来显示它们的信息。
阅读全文
相关推荐
















