# 1、练习上课作业讲解的面向对象代码,明天默写
# 2、基于上课作业讲解的面向对象代码,扩写Student类
# 3、加入序列化与反序列化操作
# 4、对象之间的关联采用id号
# 5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
import pickle
import uuid
class School:
school_name = 'OLDBOY'
def __init__(self,name,addr):
self.uid=uuid.uuid4()
self.nickname=name
self.addr = addr
self.classes = []
def related_class(self,class_obj):
self.classes.append(class_obj.uid)
def getinfo_class(self):
print(self.nickname.center(60,'-'))
for class_uid in self.classes:
with open(f'{class_uid}.pkl','rb')as f:
class_obj = pickle.load(f)
class_obj.getinfo_course()
def save(self):
with open(f'{self.uid}.pkl','wb')as f:
pickle.dump(self,f)
class Class:
def __init__(self,name):
self.uid = uuid.uuid4()
self.name = name
self.course = None
def related_course(self,course_obj):
self.course = course_obj.uid
def getinfo_course(self):
print('%s' % self.name,end=" ")
with open(f'{self.course}.pkl','rb')as f:
course_obj = pickle.load(f)
course_obj.getinfo_course()
def save(self):
with open(f'{self.uid}.pkl','wb')as f:
pickle.dump(self,f)
class Course:
def __init__(self,name,period,price):
self.uid = uuid.uuid4()
self.name = name
self.period = period
self.price = price
def getinfo_course(self):
print(f'')
def save(self):
with open(f'{self.uid}.pkl','wb')as f:
pickle.dump(self,f)
class Student:
# 学生的学校
# 学生的姓名
# 学生的年龄
# 学号
# 学生的性别
def __init__(self,school,name,age,number,gender):
self.uid = uuid.uuid4()
self.school = school
self.name = name
self.age = age
self.number = number
self.gender = gender
def getinfo_student(self):
print(f'')
def choose(self,course_obj):
self.course = course_obj.uid
def save(self):
with open(f'{self.uid}.pkl','wb')as f:
pickle.dump(self,f)
school_obj1 = School('上海校区','虹桥')
school_obj2 = School('北京校区','北京某地')
class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产29期')
course_obj1 = Course('python全栈','6mons','20000')
course_obj2 = Course('linux运维','5mons','18000')
student_obj1 = Student('上海校区','西楼','24','007','男')
student_obj2 = Student('北京校区','黑人','18','666','男')
school_obj1.related_class(class_obj1)
class_obj1.related_course(course_obj1)
school_obj1.save()
# school_obj2.save()
class_obj1.save()
# class_obj2.save()
course_obj1.save()
# course_obj2.save()
# student_obj1.save()
# student_obj2.save()
school_obj1.getinfo_class()