设计并测试一个名为 My Student 的类,该类包括以下属性:id(学号)name(姓名)以及 3 门课程 maths(数学)English(英语)computer(计算机)的成绩,类中的方法包括计算三门课程的总分,平均分和最高分 Python 编写
时间: 2023-12-30 08:06:16 浏览: 105
好的,下面是 MyStudent 类的代码实现:
```python
class MyStudent:
def __init__(self, id, name, maths, english, computer):
self.id = id
self.name = name
self.maths = maths
self.english = english
self.computer = computer
def calc_total_score(self):
return self.maths + self.english + self.computer
def calc_avg_score(self):
return self.calc_total_score() / 3
def calc_max_score(self):
return max(self.maths, self.english, self.computer)
```
这个类包含了初始化方法 `__init__` 和三个计算成绩的方法 `calc_total_score`、`calc_avg_score` 和 `calc_max_score`。
下面是一个测试代码,可以用来测试 MyStudent 类的功能:
```python
student = MyStudent("001", "张三", 80, 90, 95)
print(f"{student.name} 的总分是 {student.calc_total_score()}")
print(f"{student.name} 的平均分是 {student.calc_avg_score()}")
print(f"{student.name} 的最高分是 {student.calc_max_score()}")
```
这个测试代码创建了一个名为 `student` 的 MyStudent 对象,然后分别调用了计算总分、平均分和最高分的方法,并输出了结果。运行结果如下:
```
张三 的总分是 265
张三 的平均分是 88.33333333333333
张三 的最高分是 95
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)