在一个学校中有教师和学生两类人员。学生信息有身份证号、学号、专业、姓名、性别、年龄及数学、英语、Java程序设计三门课程的成绩。教师信息有身份证号、教师号、专业、姓名、性别、年龄及授课的学时数。创建学生和教师的对象并输出对象的信息
时间: 2024-06-16 14:06:45 浏览: 74
在一个学校中,可以创建学生和教师的对象,并输出它们的信息。下面是一个示例代码:
```python
class Person:
def __init__(self, id_card, name, gender, age):
self.id_card = id_card
self.name = name
self.gender = gender
self.age = age
class Student(Person):
def __init__(self, id_card, student_id, major, name, gender, age, math_score, english_score, java_score):
super().__init__(id_card, name, gender, age)
self.student_id = student_id
self.major = major
self.math_score = math_score
self.english_score = english_score
self.java_score = java_score
class Teacher(Person):
def __init__(self, id_card, teacher_id, major, name, gender, age, teaching_hours):
super().__init__(id_card, name, gender, age)
self.teacher_id = teacher_id
self.major = major
self.teaching_hours = teaching_hours
# 创建学生对象
student1 = Student("1234567890", "20210001", "计算机科学", "张三", "男", 20, 90, 85, 95)
student2 = Student("0987654321", "20210002", "软件工程", "李四", "女", 19, 80, 75, 85)
# 创建教师对象
teacher1 = Teacher("9876543210", "T2021001", "计算机科学", "王老师", "女", 30, 40)
teacher2 = Teacher("0123456789", "T2021002", "软件工程", "赵老师", "男", 35, 35)
# 输出学生对象的信息
print("学生1信息:")
print("身份证号:", student1.id_card)
print("学号:", student1.student_id)
print("专业:", student1.major)
print("姓名:", student1.name)
print("性别:", student1.gender)
print("年龄:", student1.age)
print("数学成绩:", student1.math_score)
print("英语成绩:", student1.english_score)
print("Java程序设计成绩:", student1.java_score)
print("\n学生2信息:")
print("身份证号:", student2.id_card)
print("学号:", student2.student_id)
print("专业:", student2.major)
print("姓名:", student2.name)
print("性别:", student2.gender)
print("年龄:", student2.age)
print("数学成绩:", student2.math_score)
print("英语成绩:", student2.english_score)
print("Java程序设计成绩:", student2.java_score)
# 输出教师对象的信息
print("\n教师1信息:")
print("身份证号:", teacher1.id_card)
print("教师号:", teacher1.teacher_id)
print("专业:", teacher1.major)
print("姓名:", teacher1.name)
print("性别:", teacher1.gender)
print("年龄:", teacher1.age)
print("授课学时数:", teacher1.teaching_hours)
print("\n教师2信息:")
print("身份证号:", teacher2.id_card)
print("教师号:", teacher2.teacher_id)
print("专业:", teacher2.major)
print("姓名:", teacher2.name)
print("性别:", teacher2.gender)
print("年龄:", teacher2.age)
print("授课学时数:", teacher2.teaching_hours)
```
这段代码定义了一个`Person`类作为学生和教师类的父类,包含了身份证号、姓名、性别和年龄等基本信息。学生类`Student`和教师类`Teacher`继承自`Person`类,并添加了各自特有的属性和方法。
创建学生和教师对象时,需要传入相应的参数来初始化对象的属性。然后通过对象的属性来输出对象的信息。
阅读全文