已有若干个学生数据,这些数据包括学号,姓名,Python程序设计成绩,高等数学成绩和大数据技术成绩,要求定义学生类,并用其成员函数修改各门课程的分数(要求将方法装饰成属性)、求各门课程的最高分、平均分。python
时间: 2024-11-14 09:22:29 浏览: 5
为了满足上述需求,我们可以定义一个名为`Student`的类,它包含学生的个人信息以及计算成绩的相关功能。首先,我们创建三个私有属性来存储每个学生的成绩,然后定义装饰器作为公共属性来修改成绩。同时,添加两个方法来分别获取最高分和平均分。
```python
class Student:
def __init__(self, id, name):
self._id = id
self._name = name
self.pythongraphics_score = 0
self.mathematics_score = 0
self.data_analysis_score = 0
# 装饰器方法,用于修改成绩
@property
def python_programming(self):
return self.pythongraphics_score
@python_programming.setter
def python_programming(self, score):
if isinstance(score, int) and score >= 0:
self.pythongraphics_score = score
else:
print("请输入有效分数")
@property
def mathematics(self):
return self.mathematics_score
@mathematics.setter
def mathematics(self, score):
if isinstance(score, int) and score >= 0:
self.mathematics_score = score
else:
print("请输入有效分数")
@property
def data_analysis(self):
return self.data_analysis_score
@data_analysis.setter
def data_analysis(self, score):
if isinstance(score, int) and score >= 0:
self.data_analysis_score = score
else:
print("请输入有效分数")
# 获取最高分方法
def get_highest_score(self):
scores = [self.pythongraphics_score, self.mathematics_score, self.data_analysis_score]
return max(scores) if scores else None
# 计算平均分方法
def calculate_average(self):
total = sum([self.pythongraphics_score, self.mathematics_score, self.data_analysis_score])
return total / len(scores) if total != 0 else 0
# 示例用法
student1 = Student(1, "张三")
student1.python_programming = 85
student1.mathematics = 93
student1.data_analysis = 90
print(f"最高分: {student1.get_highest_score()}")
print(f"平均分: {student1.calculate_average()}")
```
阅读全文