day28 作业
阅读原文时间:2023年07月10日阅读:2
import uuid
import pickle
import os
# 学校类
class School:
    #校区的名字:如"老男孩上海校区"
    #校区的地址:如"上海虹桥"
    def __init__(self,name,area):
        self.name = name
        self.area = area
        self.classlist = []
    def save(self):
        with open(f'school/{self.name}.pickle','wb') as f:
            pickle.dump(self,f)
    def tell_class(self):
        print(f'{self.name}:')
        for class_id in self.classlist:
            with open(f'class/{class_id}.pickle', 'rb') as f:
                class_obj = pickle.load(f)
                class_obj.tell_class()

    #校区创建完毕后,可以为每个校区关联班级
    def choose_class(self,class_obj):
        self.classlist.append(class_obj.id)

# 教室类
class SchoolClass:
    #班级名字
    #班级所在校区
    def __init__(self,name,campus):
        self.id = uuid.uuid4()
        self.name = name
        self.campus = campus
        self.course_list = []
    #班级创建完毕后,可以为每个班级关联课程
    def choose_course(self,course_obj):
        self.course_list.append(course_obj.id)
    def save(self):
        with open(f'class/{self.id}.pickle','wb') as f:
            pickle.dump(self,f)
    def tell_class(self):
        print(f'班级名称:{self.name} 班级校区:{self.campus}')
# 课程类
class Course:
    #课程名字
    #课程周期
    #课程价格
    def __init__(self,cou_name,circle,price):
        self.course_name = cou_name
        self.id = uuid.uuid4()
        self.circle = circle
        self.price = price
    def save(self):
        with open(f'course/{self.id}.pickle','wb') as f:
            pickle.dump(self,f)
    def tell_course(self):
        print(f'{self.course_name},{self.circle},{self.price}')
# 学生类
class Student:
    def __init__(self,s_school,stu_name,age,sno,gender):
        #学生的学校
        #学生的姓名
        #学生的年龄
        #学号
        #学生的性别
        self.id = uuid.uuid4()
        self.student_school = s_school
        self.student_name = stu_name
        self.student_age = age
        self.student_sno = sno
        self.student_gender = gender
    #学生创建完毕后,学生可以选择班级
    def choose_class(self,class_name):
        self.class_name = class_name
    def save(self):
        with open(f'Student/{self.id}.pickle','wb') as f:
            pickle.dump(self,f)
# 老师类
class Teacher:
    def __init__(self,t_name,t_age,salary,level):
        #老师的名字
        #老师的年龄
        #老师的薪资
        #老师的等级
        self.id = uuid.uuid4()
        self.teacheer_name = t_name
        self.teacheer_age = t_age
        self.teacheer_salary = salary
        self.teacheer_level = level
        #老师创建完毕后,可以为学生打分
    def save(self):
        with open(f'Teacher/{self.id}.pickle','wb') as f:
            pickle.dump(self,f)
    def student_grade(self,student_obj,grade):
        student_obj.grade = grade

# 创建学校对象
school_obj = School('老男孩','上海')

# 序列化保存学校信息
school_obj.save()

# 创建班级对象
class_obj = SchoolClass('脱产14期','上海')

# 序列化保存班级信息
class_obj.save()

# 关联学校和班级
school_obj.choose_class(class_obj)

# 反序列化得到班级信息
school_obj.tell_class()