2)根据类的继承原理,设计Person类的一个子类,叫做学生类(Student 类),该类主要对学生的个人信息及成绩情况做管理,能够进行个人信息及学科成绩各项信息的录入和输出,还可以统计学生个人平均分,统计学生个人最高分及最低分,统计某班级所有学生平均分,统计某班级不及格人数。 属性:学号、班级、科目及对应成绩 方法:信息的输入、信息的输出、个人平均分统计方法、个人最高分及最低分统计方法、个人不及格科目和成绩统计,个人科目成绩排序。
时间: 2024-04-03 14:30:55 浏览: 84
好的,根据您的要求,我为您设计一个Person类的子类——Student类,满足您的需求。
```python
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, gender, student_id, class_name):
super().__init__(name, age, gender)
self.student_id = student_id
self.class_name = class_name
self.scores = {}
def input_score(self, subject, score):
self.scores[subject] = score
def output_score(self):
for subject, score in self.scores.items():
print("{}的{}成绩为{}".format(self.name, subject, score))
def average_score(self):
return sum(self.scores.values()) / len(self.scores)
def max_min_score(self):
max_score = max(self.scores.values())
min_score = min(self.scores.values())
max_subject = [subject for subject, score in self.scores.items() if score == max_score]
min_subject = [subject for subject, score in self.scores.items() if score == min_score]
print("{}的最高分为{},科目为{}".format(self.name, max_score, max_subject))
print("{}的最低分为{},科目为{}".format(self.name, min_score, min_subject))
def fail_subject(self):
fail_subjects = [subject for subject, score in self.scores.items() if score < 60]
if fail_subjects:
print("{}的不及格科目及成绩为{}".format(self.name, dict(zip(fail_subjects, [self.scores[subject] for subject in fail_subjects]))))
else:
print("{}没有不及格科目".format(self.name))
@staticmethod
def average_score_of_class(students):
scores = []
for student in students:
scores.extend(student.scores.values())
return sum(scores) / len(scores)
@staticmethod
def fail_number_of_class(students):
fail_number = 0
for student in students:
if any(score < 60 for score in student.scores.values()):
fail_number += 1
return fail_number
def sort_by_score(self):
sorted_scores = sorted(self.scores.items(), key=lambda x: x[1], reverse=True)
print("{}的成绩按照从高到低排序为{}".format(self.name, sorted_scores))
```
在这个类中,我们继承了Person类,然后添加了学号、班级和成绩的属性,以及输入、输出、平均分、最高分/最低分、不及格科目和成绩统计、班级平均分、班级不及格人数和成绩排序等方法。这个类可以很好地管理学生的个人信息和成绩情况,并且可以用于班级管理。
阅读全文