练习:面向对象编程其二

# 1、练习上课作业讲解的面向对象代码,明天默写
# 2、基于上课作业讲解的面向对象代码,扩写Student类
# 3、加入序列化与反序列化操作
# 4、对象之间的关联采用id号
# 5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象

import uuid
import pickle

school_dict = {}


class School:
    total = '中国技校联盟'

    def __init__(self, name, addr):
        self.uid = uuid.uuid4()
        self.name = name
        self.addr = addr
        self.grade_dict = {}

    def add_grade(self, grade_obj):
        self.grade_dict[grade_obj.uid] = grade_obj

    def add_self(self, self_dict):
        self_dict[self.uid] = self

    def save(self):
        with open(r'schools\{}.pkl'.format(self.uid), 'wb')as f:
            pickle.dump(self, f)


class Grade:
    def __init__(self, name):
        self.uid = uuid.uuid4()
        self.name = name
        self.course_dict = {}
        self.stu_dict = {}
        self.tch_dict = {}

    def add_course(self, course_obj):
        self.course_dict[course_obj.uid] = course_obj

    def add_stu(self, stu_obj):
        self.stu_dict[stu_obj.uid] = stu_obj

    def add_tch(self, tch_obj):
        self.tch_dict[tch_obj.uid] = tch_obj

    def save(self):
        with open(r'grades\{}.pkl'.format(self.uid), 'wb')as f:
            pickle.dump(self, f)


class Course:
    def __init__(self, name, time, price):
        self.uid = uuid.uuid4()
        self.name = name
        self.time = time
        self.price = price

    def save(self):
        with open(r'courses\{}.pkl'.format(self.uid), 'wb')as f:
            pickle.dump(self, f)


class Student:
    def __init__(self, name, age, gender):
        self.uid = uuid.uuid4()
        self.name = name
        self.age = age
        self.gender = gender

    def save(self):
        with open(r'students\{}.pkl'.format(self.uid), 'wb')as f:
            pickle.dump(self, f)


class Teacher:
    def __init__(self, name, level):
        self.uid = uuid.uuid4()
        self.name = name
        self.level = level
        self.task_list = []

    def add_task(self, task):
        self.task_list.append(task)

    def save(self):
        with open(r'teachers\{}.pkl'.format(self.uid), 'wb')as f:
            pickle.dump(self, f)


course_obj_1 = Course('挖掘机基础', '10days', '1000')
course_obj_1.save()
course_obj_2 = Course('挖掘机进阶', '20days', '2000')
course_obj_2.save()
stu_obj_1 = Student('tom', '18', 'male')
stu_obj_1.save()
stu_obj_2 = Student('jan', '19', 'female')
stu_obj_2.save()
tch_obj_1 = Teacher('egon', '无双')
tch_obj_1.add_task('挖掘机基础(A)')
tch_obj_1.add_task('挖掘机基础(B)')
tch_obj_1.save()
tch_obj_2 = Teacher('tank', '化境')
tch_obj_2.add_task('挖掘机进阶(A)')
tch_obj_2.add_task('挖掘机进阶(B)')
tch_obj_2.save()
grade_obj_1 = Grade('挖掘机速成班')
grade_obj_1.add_course(course_obj_1)
grade_obj_1.add_course(course_obj_2)
grade_obj_1.add_stu(stu_obj_1)
grade_obj_1.add_stu(stu_obj_2)
grade_obj_1.add_tch(tch_obj_1)
grade_obj_1.add_tch(tch_obj_2)
grade_obj_1.save()
school_obj_1 = School('帅比技校', '土堆子村')
school_obj_1.add_grade(grade_obj_1)
school_obj_1.add_self(school_dict)
school_obj_1.save()

with open(r'school_dict.pkl', 'wb')as f:
    pickle.dump(school_dict, f)

 

你可能感兴趣的:(练习:面向对象编程其二)