设计一个表示学生的类,里面有学生的学号、姓名,还要有三项成绩:数学成绩、Python成绩、计算机网络成绩,要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。设计要求如下:所有属性必须封装,设置为私有数据
时间: 2024-05-29 19:11:04 浏览: 69
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
class Student:
def __init__(self, student_id, name, math_score, python_score, computer_score):
self.__student_id = student_id
self.__name = name
self.__math_score = math_score
self.__python_score = python_score
self.__computer_score = computer_score
def get_student_id(self):
return self.__student_id
def get_name(self):
return self.__name
def get_math_score(self):
return self.__math_score
def get_python_score(self):
return self.__python_score
def get_computer_score(self):
return self.__computer_score
def get_total_score(self):
return self.__math_score + self.__python_score + self.__computer_score
def get_average_score(self):
return self.get_total_score() / 3
def get_highest_score(self):
return max(self.__math_score, self.__python_score, self.__computer_score)
def get_lowest_score(self):
return min(self.__math_score, self.__python_score, self.__computer_score)
def __str__(self):
return 'Student ID: {}\nName: {}\nMath Score: {}\nPython Score: {}\nComputer Score: {}\nTotal Score: {}\nAverage Score: {}\nHighest Score: {}\nLowest Score: {}'.format(
self.__student_id, self.__name, self.__math_score, self.__python_score, self.__computer_score,
self.get_total_score(), self.get_average_score(), self.get_highest_score(), self.get_lowest_score()
)
阅读全文