定义一个student类,有姓名、学生学号、班级、所学3门课程(课程可以自由挑选)等属性,并且有初始化student对象的构造、方法以及统计3科成绩总和的成员方法。创建一个学生对象,并显示该对象的所有信息。
时间: 2024-02-03 10:02:17 浏览: 26
好的,以下是符合你要求的代码:
```python
class Student:
def __init__(self, name, stu_id, classname, course1, course2, course3):
self.name = name
self.stu_id = stu_id
self.classname = classname
self.course1 = course1
self.course2 = course2
self.course3 = course3
def show_info(self):
print("姓名:", self.name)
print("学号:", self.stu_id)
print("班级:", self.classname)
print("所学课程:", self.course1, self.course2, self.course3)
def total_score(self):
return self.course1 + self.course2 + self.course3
# 创建学生对象
student1 = Student("张三", "20210001", "计算机科学与技术1班", 90, 85, 92)
# 显示学生信息
student1.show_info()
# 统计成绩总和
total_score = student1.total_score()
print("总成绩:", total_score)
```
运行上述代码后,将会输出以下信息:
```
姓名: 张三
学号: 20210001
班级: 计算机科学与技术1班
所学课程: 90 85 92
总成绩: 267
```
希望这个代码可以帮助到你!