python利用类方法,编写学生成绩管理系统。定义学生类,包含学号、姓名、英语成绩、python成绩、java成绩、c语言成绩、总成绩。系统有增删查改功能。
时间: 2023-07-10 11:27:55 浏览: 81
python语言实现学生成绩管理系统
5星 · 资源好评率100%
好的,我可以为您提供一个简单的学生成绩管理系统的代码示例。代码如下:
```python
class Student:
# 学生类
def __init__(self, id, name, english_score, python_score, java_score, c_score):
self.id = id
self.name = name
self.english_score = english_score
self.python_score = python_score
self.java_score = java_score
self.c_score = c_score
self.total_score = english_score + python_score + java_score + c_score
def __str__(self):
return f'{self.id} {self.name} {self.english_score} {self.python_score} {self.java_score} {self.c_score} {self.total_score}'
class StudentManager:
# 学生成绩管理类
students = []
@classmethod
def add_student(cls, student):
cls.students.append(student)
@classmethod
def remove_student(cls, id):
for student in cls.students:
if student.id == id:
cls.students.remove(student)
return
@classmethod
def query_student(cls, id):
for student in cls.students:
if student.id == id:
return student
@classmethod
def modify_student(cls, id, name, english_score, python_score, java_score, c_score):
for student in cls.students:
if student.id == id:
student.name = name
student.english_score = english_score
student.python_score = python_score
student.java_score = java_score
student.c_score = c_score
student.total_score = english_score + python_score + java_score + c_score
return
@classmethod
def get_all_students(cls):
return cls.students
# 测试
if __name__ == '__main__':
# 添加学生
StudentManager.add_student(Student('001', '张三', 80, 90, 85, 95))
StudentManager.add_student(Student('002', '李四', 75, 85, 90, 92))
StudentManager.add_student(Student('003', '王五', 90, 80, 95, 85))
# 查询学生
student = StudentManager.query_student('001')
print(student)
# 修改学生
StudentManager.modify_student('001', '张三丰', 85, 95, 90, 80)
student = StudentManager.query_student('001')
print(student)
# 删除学生
StudentManager.remove_student('002')
students = StudentManager.get_all_students()
for student in students:
print(student)
```
上述代码中,定义了一个学生类和一个学生成绩管理类。学生类包含了学号、姓名、英语成绩、Python成绩、Java成绩、C语言成绩和总成绩属性,以及初始化方法和字符串表示方法。学生成绩管理类包括了增加学生、删除学生、查询学生、修改学生和获取所有学生的方法,都是类方法。在测试部分中,我们演示了如何使用这个学生成绩管理系统。
阅读全文