作业:
class School:
school_name = 'OLDBOY'
def __init__(self, nickname, addr):
self.uuid = uuid.uuid4()
self.nickname = nickname
self.addr = addr
self.classes = []
self.save_info()
# 关联班级对象
def related_class(self, class_obj):
self.classes.append(class_obj.uuid)
self.save_info()
# 获取班级信息
def get_class(self):
# 打印的班级的名称
print(self.nickname.center(60, '='))
for class_uuid in self.classes:
with open(f'class{class_uuid}.pkl', 'rb') as f:
class_obj = pickle.load(f)
class_obj.get_course()
class_obj.get_student()
class_obj.get_teacher()
# 保存学校对象
def save_info(self):
with open(f'school{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)
# 打印学校信息
def select(self):
with open(f'school{self.uuid}.pkl', 'rb') as f:
pickle.load(f)
class Class:
def __init__(self, name):
self.uuid = uuid.uuid4()
self.name = name
self.course = None
self.students = []
self.teachers = []
self.save_info()
# 关联课程id
def related_course(self, course_obj):
self.course = course_obj.uuid
self.save_info()
# 关联学生id给班级
def related_student(self, stu_obj):
self.students.append(stu_obj.uuid)
self.save_info()
# 关联老师id给班级
def related_teacher(self, tea_obj):
self.teachers.append(tea_obj.uuid)
self.save_info()
# 获取课程对象
def get_course(self):
print('%s' % self.name, end=" ")
with open(f'course{self.course}.pkl', 'rb') as f:
course_obj = pickle.load(f)
course_obj.tell_cou_info()
# 获取学生对象
def get_student(self):
for stu_obj_uuid in self.students:
with open(f'student{stu_obj_uuid}.pkl', 'rb') as f:
stu_obj = pickle.load(f)
stu_obj.tell_stu_info()
# 获取老师对象
def get_teacher(self):
for tea_obj_uuid in self.teachers:
with open(f'teacher{tea_obj_uuid}.pkl', 'rb') as f:
tea_obj = pickle.load(f)
tea_obj.tell_tea_info()
# 保存班级信息
def save_info(self):
with open(f'class{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)
class Course:
def __init__(self, name, period, price):
self.uuid = uuid.uuid4()
self.name = name
self.period = period
self.price = price
self.save_info()
# 打印课程信息
def tell_cou_info(self):
print('<课程名:%s 周期:%s 价钱:%s>' % (self.name, self.period, self.price))
# 保存课程信息
def save_info(self):
with open(f'course{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)
class Student:
def __init__(self, name, age, sex):
self.uuid = uuid.uuid4()
self.name = name
self.age = age
self.sex = sex
self.save_info()
# 打印学生信息
def tell_stu_info(self):
print(f'学生姓名:{self.name} 学生年龄:{self.age} 学生性别:{self.sex}')
# 保存学生信息
def save_info(self):
with open(f'student{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)
class Teacher:
def __init__(self, name, age, salary, grade):
self.uuid = uuid.uuid4()
self.name = name
self.age = age
self.salary = salary
self.grade = grade
self.save_info()
# 打印老师信息
def tell_tea_info(self):
print(f'老师姓名:{self.name} 老师年龄:{self.age} 老师薪资:{self.salary} 老师等级:{self.grade}')
# 保存老师信息
def save_info(self):
with open(f'teacher{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)
# 创建学校对象
school_obj1 = School('老男孩魔都校区', '上海')
school_obj2 = School('老男孩帝都校区', '北京')
# 创建班级对象
class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产15期')
# 创建课程对象
course_obj1 = Course('python全栈开发', '6mons', 20000)
course_obj2 = Course('linux运维', '5mons', 20000)
# 创建学生对象
stu_obj1 = Student('egon', 18, 'female')
stu_obj2 = Student('tank', 18, 'male')
stu_obj3 = Student('alex', 18, 'male')
# 创建老师对象
teacher_obj1 = Teacher('egon', 88, 30000, '最美人民女教师')
teacher_obj2 = Teacher('tank', 18, 300000, '最帅人民教师')
# 为学校开设班级
# 上海校区开了:脱产14期,上海校区开了脱产15期
school_obj1.related_class(class_obj1)
school_obj1.related_class(class_obj2)
# 北京校区开了:脱产29期
school_obj2.related_class(class_obj3)
# 为班级关联课程对象
class_obj1.related_course(course_obj1)
class_obj2.related_course(course_obj2)
class_obj3.related_course(course_obj1)
# 为班级关联学生对象
class_obj1.related_student(stu_obj1)
class_obj2.related_student(stu_obj2)
class_obj3.related_student(stu_obj3)
# 为班级关联老师对象
class_obj1.related_teacher(teacher_obj1)
class_obj2.related_teacher(teacher_obj2)
class_obj3.related_teacher(teacher_obj1)
school_obj1.get_class()
school_obj2.get_class()