Python设计并测试一个名为MyStudent的类,该类包括以下属性:id,name,以及三门课程math、english、computer的成绩绩,类中的方法包括3门课程的总分、平均分和最高分并显示
时间: 2024-05-08 09:18:34 浏览: 12
好的,我明白了。下面是 Python 的代码实现 MyStudent 类:
```python
class MyStudent:
def __init__(self, id, name, math_score, english_score, computer_score):
self.id = id
self.name = name
self.math_score = math_score
self.english_score = english_score
self.computer_score = computer_score
def get_total_score(self):
total_score = self.math_score + self.english_score + self.computer_score
return total_score
def get_average_score(self):
total_score = self.get_total_score()
average_score = total_score / 3
return average_score
def get_highest_score(self):
scores = [self.math_score, self.english_score, self.computer_score]
highest_score = max(scores)
return highest_score
student = MyStudent(1001, "小明", 90, 85, 95)
print("学生信息:", student.id, student.name)
print("数学成绩:", student.math_score)
print("英语成绩:", student.english_score)
print("计算机成绩:", student.computer_score)
print("总分:", student.get_total_score())
print("平均分:", student.get_average_score())
print("最高分:", student.get_highest_score())
```
这个程序中定义了一个 MyStudent 类,用于存储学生的成绩信息。在类的初始化方法中,我们传入了学生的 id、name 和三门科目的成绩。然后,我们定义了三个方法,用于计算总分、平均分和最高分。在这三个方法中,我们分别用到了类的属性和其他方法。最后,在主程序中,我们创建了一个 MyStudent 类的实例,然后调用了其各个方法,输出了学生的成绩信息。