3. 编写一个学生和教师数据输入和显示程序(使用构造函数)。学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名的输入和显示设计成一个类person,并作为学生类student和教师类teacher的基类。
时间: 2023-06-14 20:06:01 浏览: 50
以下是该程序的实现:
```python
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
def display(self):
print("ID:", self.id)
print("Name:", self.name)
class Student(Person):
def __init__(self, id, name, class_num, score):
super().__init__(id, name)
self.class_num = class_num
self.score = score
def display(self):
super().display()
print("Class:", self.class_num)
print("Score:", self.score)
class Teacher(Person):
def __init__(self, id, name, title, department):
super().__init__(id, name)
self.title = title
self.department = department
def display(self):
super().display()
print("Title:", self.title)
print("Department:", self.department)
# 示例
student1 = Student(1001, "张三", "一班", 90)
student1.display()
teacher1 = Teacher(2001, "李四", "教授", "计算机科学与技术")
teacher1.display()
```
运行结果:
```
ID: 1001
Name: 张三
Class: 一班
Score: 90
ID: 2001
Name: 李四
Title: 教授
Department: 计算机科学与技术
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)