1.练习上课作业讲解的面向对象代码,明天默写
2.基于上课作业讲解的面向对象代码,扩写Student类
3.加入序列化与反序列化操作
4.对象之间的关联采用id号
5.可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
import uuid
import pickle
# 学校(校区名、地址、关联班级列表)
class School:
def __init__(self, name, address):
self.name = name
self.address = address
self.class_list = []
def show_school_info(self):
print(f'校区信息 | 名称:{self.name} 地址:{self.address}')
for class_id in self.class_list:
with open(f'Class/{class_id}.pickle', mode='rb') as f:
class_obj = pickle.load(f)
class_obj.show_class_info()
def save(self):
with open(f'School/{self.name}.pickle', mode='wb') as f:
pickle.dump(self, f)
# 校区创建完毕后,可以为每个校区关联班级
def related_class(self, class_obj):
self.class_list.append(class_obj.id)
# 班级(名称、所在校区、关联课程列表)
class Class:
def __init__(self, name, school):
self.id = uuid.uuid4()
self.name = name
self.school = school
self.course_list = []
def show_class_info(self):
print(f' 班级信息 | 名称:{self.name} 校区:{self.school}')
def save(self):
with open(f'Class/{self.id}.pickle', mode='wb') as f:
pickle.dump(self, f)
# 班级创建完毕后,可以为每个班级关联课程
def related_course(self, course_obj):
self.course_list.append(course_obj.id)
# 课程(课程名、周期、价格)
class Course:
def __init__(self, name, period, price):
self.id = uuid.uuid4()
self.name = name
self.period = period
self.price = price
def show_course_info(self):
print(f'课程信息 | 名称:{self.name} 周期:{self.period} 价格:{self.price}')
def save(self):
with open(f'Course/{self.id}.pickle', mode='wb') as f:
pickle.dump(self, f)
# 学生(所在校区、名字、年龄、学号、性别、选择课程)
class Student:
def __init__(self, school, name, age, stu_id, gender):
self.id = uuid.uuid4()
self.school = school
self.name = name
self.age = age
self.stu_id = stu_id
self.gender = gender
def show_student_info(self):
print(f'学生信息 | 姓名:{self.name} 校区:{self.school} 年龄:{self.age} 学号:{self.stu_id} 性别:{self.gender}')
def save(self):
with open(f'Student/{self.id}.pickle', mode='wb') as f:
pickle.dump(self, f)
def choose_class(self, class_name):
self.class_name = class_name
# 老师(名字、年龄、薪资、等级、给学生评分)
class Teacher:
def __init__(self, name, age, salary, level):
self.id = uuid.uuid4()
self.name = name
self.age = age
self.salary = salary
self.level = level
def show_teacher_info(self):
print(f'老师信息 | 姓名:{self.name} 年龄:{self.age} 薪资:{self.salary} 等级:{self.level}')
def save(self):
with open(f'Teacher/{self.id}.pickle', mode='wb') as f:
pickle.dump(self, f)
def student_score(self, student_obj, score):
student_obj.score = score
# 创建学校对象(校区名、地址、关联班级列表)
school_obj1 = School('老男孩上海校区', '上海市青浦区')
school_obj2 = School('老男孩北京校区', '北京市海淀区')
# 序列化保存学校信息
school_obj1.save()
school_obj2.save()
# 创建班级对象(名称、所在校区)
class_obj1 = Class('脱产14期', '上海校区')
class_obj2 = Class('脱产15期', '上海校区')
class_obj3 = Class('脱产29期', '北京校区')
# 序列化保存班级信息
class_obj1.save()
class_obj2.save()
class_obj3.save()
# 关联学校和班级
school_obj1.related_class(class_obj1)
school_obj1.related_class(class_obj2)
school_obj2.related_class(class_obj3)
# 反序列化得到班级信息
school_obj1.show_school_info()
school_obj2.show_school_info()
# 创建课程对象(课程名、周期、价格)
course_obj1 = Course('egon养成培训', '6-month', 10)
course_obj2 = Course('单身狗的养成', '3-month', 998)
# 序列化保存学校信息
course_obj1.save()
course_obj2.save()
# 创建学生对象(所在校区、名字、年龄、学号、性别、选择课程)
student_obj1 = Student('老男孩上海校区', '真·egon真身', 18, 2020001, 'male')
student_obj2 = Student('老男孩上海校区', 'egon试验品1', 19, 2020002, 'male')
student_obj3 = Student('老男孩北京校区', 'egon试验品2', 78, 2020003, 'female')
# 序列化保存班级信息
student_obj1.save()
student_obj2.save()
student_obj3.save()
# 关联学校和班级
student_obj1.choose_class(course_obj1)
student_obj2.choose_class(course_obj1)
student_obj3.choose_class(course_obj2)
# 反序列化得到班级信息
student_obj1.show_student_info()
student_obj2.show_student_info()
student_obj3.show_student_info()