定义一个描述学生的类student,数据成员包括学号,姓名,性别,家庭住址,联系电话,邮箱,五门课的成绩
时间: 2024-01-05 09:02:25 浏览: 98
下面是一个Python的学生类的代码示例:
```python
class Student:
def __init__(self, student_id, name, gender, address, phone, email, score1, score2, score3, score4, score5):
self.student_id = student_id
self.name = name
self.gender = gender
self.address = address
self.phone = phone
self.email = email
self.score1 = score1
self.score2 = score2
self.score3 = score3
self.score4 = score4
self.score5 = score5
def get_total_score(self):
return self.score1 + self.score2 + self.score3 + self.score4 + self.score5
def get_average_score(self):
return self.get_total_score() / 5
def __str__(self):
return f"Student ID: {self.student_id}\nName: {self.name}\nGender: {self.gender}\nAddress: {self.address}\n" \
f"Phone: {self.phone}\nEmail: {self.email}\nScore 1: {self.score1}\nScore 2: {self.score2}\n" \
f"Score 3: {self.score3}\nScore 4: {self.score4}\nScore 5: {self.score5}\n"
```
在这个类中,我们定义了一个构造函数 `__init__` 来初始化学生对象的数据成员。同时,还定义了两个方法 `get_total_score` 和 `get_average_score` 来计算总成绩和平均成绩。最后,我们还重写了类的 `__str__` 方法,用于输出学生对象的信息。
阅读全文