python设计并测试一个名为MyStudent的类。该类包括一下属性:id(学号)、name(姓名)、以及3们课程maths(数学)、english(英语)、computer(计算机)的成绩,类中的方法包括计算三门课程的总分、平均分和最高分
时间: 2024-02-05 11:03:22 浏览: 66
好的,以下是MyStudent类的Python代码实现:
```python
class MyStudent:
def __init__(self, id, name, maths_score, english_score, computer_score):
self.id = id
self.name = name
self.maths_score = maths_score
self.english_score = english_score
self.computer_score = computer_score
def total_score(self):
return self.maths_score + self.english_score + self.computer_score
def average_score(self):
return self.total_score() / 3
def highest_score(self):
return max(self.maths_score, self.english_score, self.computer_score)
```
接下来,我们可以创建一个MyStudent对象,并调用其方法:
```python
student1 = MyStudent("001", "Alice", 90, 85, 95)
print(student1.total_score()) # 输出:270
print(student1.average_score()) # 输出:90.0
print(student1.highest_score()) # 输出:95
```
以上代码中,我们创建了一个名为student1的MyStudent对象,其学号为"001",姓名为"Alice",数学、英语、计算机成绩分别为90、85、95。然后,我们分别调用了total_score()、average_score()和highest_score()方法,分别输出了总分、平均分和最高分。
阅读全文