设计一个表示学生的类,里面有学生的学号、姓名,还要有三项成绩:数学成绩、Python成绩、计算机网络成绩,要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。设计要求如下:所有属性必须封装,设置为私有数据
时间: 2024-05-29 17:11:05 浏览: 80
python创建学生成绩管理系统
5星 · 资源好评率100%
class Student:
def __init__(self, id, name, math_score, python_score, network_score):
self.__id = id
self.__name = name
self.__math_score = math_score
self.__python_score = python_score
self.__network_score = network_score
def get_id(self):
return self.__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_network_score(self):
return self.__network_score
def get_total_score(self):
return self.__math_score + self.__python_score + self.__network_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.__network_score)
def get_lowest_score(self):
return min(self.__math_score, self.__python_score, self.__network_score)
def get_info(self):
return f"ID: {self.__id}, Name: {self.__name}, Math score: {self.__math_score}, Python score: {self.__python_score}, Network score: {self.__network_score}"
# Example usage
student1 = Student("123456", "Alice", 80, 90, 85)
print(student1.get_info())
print(f"Total score: {student1.get_total_score()}, Average score: {student1.get_average_score()}, Highest score: {student1.get_highest_score()}, Lowest score: {student1.get_lowest_score()}")
阅读全文