封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩;求总分,平均分,以及打印输出学生的相关信息
时间: 2024-01-04 15:04:17 浏览: 76
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
以下是一个简单的学生类的封装示例:
```python
class Student:
def __init__(self, name, age, gender, english_score, math_score, chinese_score):
self.name = name
self.age = age
self.gender = gender
self.english_score = english_score
self.math_score = math_score
self.chinese_score = chinese_score
def total_score(self):
return self.english_score + self.math_score + self.chinese_score
def avg_score(self):
return self.total_score() / 3
def print_info(self):
print(f"姓名:{self.name},年龄:{self.age},性别:{self.gender}")
print(f"英语成绩:{self.english_score},数学成绩:{self.math_score},语文成绩:{self.chinese_score}")
print(f"总分:{self.total_score()},平均分:{self.avg_score()}")
# 示例
s = Student("张三", 18, "男", 80, 90, 85)
s.print_info()
```
输出结果为:
```
姓名:张三,年龄:18,性别:男
英语成绩:80,数学成绩:90,语文成绩:85
总分:255,平均分:85.0
```
阅读全文