编写完整代码:实现学生类(学号、姓名、性别、年龄、班级),成绩类(学号,课程编号,成绩)、课程类(课程编号,课程名称,学分)并实现这三类的增删改查 Python
时间: 2024-09-09 07:00:50 浏览: 48
当然,我可以帮你编写一个简单的Python代码示例,用于实现学生、成绩和课程这三个类,并提供基本的CRUD操作。这里我们将使用面向对象的方式来进行设计:
```python
class Student:
def __init__(self, id, name, gender, age, class_name):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.class_name = class_name
self.grades = {}
def add_grade(self, course_id, score):
self.grades[course_id] = score
def delete_grade(self, course_id):
if course_id in self.grades:
del self.grades[course_id]
def update_grade(self, course_id, new_score):
if course_id in self.grades:
self.grades[course_id] = new_score
def view_grades(self):
return self.grades
class Course:
def __init__(self, id, name, credits):
self.id = id
self.name = name
self.credits = credits
class Grade:
def __init__(self, student, course, score):
self.student = student
self.course = course
self.score = score
# 示例数据
student1 = Student(1, "张三", "男", 18, "一班")
course1 = Course(1, "数学", 4)
grade1 = Grade(student1, course1, 90)
# 增加一个成绩到学生
student1.add_grade(course1.id, grade1.score)
# 查看学生的所有成绩
print(student1.view_grades())
# 删除成绩
student1.delete_grade(course1.id)
# 更新成绩
new_score = 95
student1.update_grade(course1.id, new_score)
# 相关问题--
阅读全文