设计一个学生类student,它具有的私有数据成员是:学号、姓名、数学、英语、计算机成绩;具有的公有成员函数是:求3门课总成绩的函数sum;求3门课平均成绩的函数average;显示学生数据信息的函数
时间: 2024-06-10 22:06:26 浏览: 85
showInfo。请使用Python语言实现该类的代码。
以下是Python代码:
```
class Student:
def __init__(self, sid, name, math_score, eng_score, comp_score):
self.__sid = sid
self.__name = name
self.__math_score = math_score
self.__eng_score = eng_score
self.__comp_score = comp_score
def sum(self):
return self.__math_score + self.__eng_score + self.__comp_score
def average(self):
return self.sum() / 3.0
def showInfo(self):
print("Student ID:", self.__sid)
print("Name:", self.__name)
print("Math Score:", self.__math_score)
print("English Score:", self.__eng_score)
print("Computer Score:", self.__comp_score)
print("Total Score:", self.sum())
print("Average Score:", self.average())
```
注意:私有数据成员使用双下划线开头表示。函数sum和average通过调用私有数据成员计算总成绩和平均成绩。公有函数showInfo用于显示学生的信息,包括学号、姓名、三门课的成绩、总成绩和平均成绩。
阅读全文
相关推荐


















