Python 设计一个名为MyStudent的类,该类包含以下属性;id(学号),name(姓名),以及三门课math(数学)English(英语)computer(计算机)的成绩,类中的方法包括3门课程的总分,平均分,和最高分并显示
时间: 2024-02-07 13:02:29 浏览: 24
非常感谢您的提问。
以下是 Python 中实现 MyStudent 类的示例代码:
```
class MyStudent:
def __init__(self, id, name, math, english, computer):
self.id = id
self.name = name
self.math = math
self.english = english
self.computer = computer
def total_score(self):
return self.math + self.english + self.computer
def average_score(self):
return self.total_score() / 3
def highest_score(self):
return max(self.math, self.english, self.computer)
def display_score(self):
print("学号:", self.id)
print("姓名:", self.name)
print("数学成绩:", self.math)
print("英语成绩:", self.english)
print("计算机成绩:", self.computer)
print("总分:", self.total_score())
print("平均分:", self.average_score())
print("最高分:", self.highest_score())
```
创建一个 MyStudent 对象,并使用 display_score 方法来输出结果:
```
student1 = MyStudent("001", "张三", 78, 85, 92)
student1.display_score()
```
输出结果如下:
```
学号: 001
姓名: 张三
数学成绩: 78
英语成绩: 85
计算机成绩: 92
总分: 255
平均分: 85.0
最高分: 92
```
希望这个答案对您有帮助。如果您有其他问题,请随时提出。