import pickle import uuid class People(object): school_name = "oldboy" def __init__(self, name, age, gender): self.__name = name self.__age = age self.__gender = gender self.u = uuid.uuid4() @property def name(self): return self.__name @name.setter def name(self, name): if type(name) IS not str: Print ( ' must be passed in str type ' ) return self.name = name the @Property DEF Age (Self): return Self. __age @ age.setter DEF Age (Self, Age): IF of the type (Age) IS Not int: Print ( ' must pass type int ' ) return self.age = Age @Property DEF Gender (Self): return self.__gender @gender.setter def gender(self, gender): if type(gender) is not str: print('必须传入str类型') return self.gender = gender # 保存数据 def save(self): with open("flies.pkl".format(self.u), "wb") as f1: pickle.dump("flies.pkl", f1) with open("flies.pkl" .Format (self.u), " rb " ) AS F2: pickle.load (F2) # define a class school class School (Object): # define the name and address of the campus classroom list DEF __init__ (Self, the Nickname, addr) : self.nickname = Nickname self.addr = addr self.u = uuid.uuid4 () self.classes = [] # define classroom DEF related_class (Self, CLASS_OBJ): self.classes.append (CLASS_OBJ) # define the print information classroom DEF tell_class (Self): Print (self.nickname.center (20, " - " )) for CLASS_OBJ in self.classes: class_obj.tell_course () # define a class class class Class (Object): # define classroom name DEF __init__ (Self, name): self.name = name self.u = uuid.uuid4 () self.course = None # define curriculum def related_course (Self, course_obj): self.course = course_obj # print course information def tell_course(self): print("%s" % self.name, end=" ") self.course.tell_info() # 保存数据 def save(self): with open("flies.pkl".format(self.u), "wb") as f1: pickle.dump("flies.pkl", f1) with open("flies.pkl".format(self.u), "rb") as f2: pickle.the Load (F2) classdefine a class curriculum# Course, (Object): # define the course name cycle DEF __init__ (Self, name, period,. Price): self.name = name self.period = period self.price = . Price self.u = uuid.uuid4 () DEF tell_info (Self ): Print ( " course name:% s curriculum cycle:% s courses price:% s " % (self.name, self.period, self.price)) # save data DEF the save (Self): with Open ( " Flies the .pkl " .format (self.u), " wb" ) AS f1: the pickle.dump ( " flies.pkl " , f1) with Open ( " flies.pkl " .format (self.u), " rb " ) AS F2: pickle.load (F2) # definition of a student class class Student (People): DEF the __init__ (Self, number, name, Age, Gender): . People the __init__ (Self, name, Age, Gender) self.number = number DEF tell_student (Self): Print ( " Science number: name% d:% s Age:% d gender:% s " % (self.number, self.name, self.age, self.gender)) class Teacher(People): def __init__(self, name, age, gender, salary, level): People.__init__(self, name, age, gender) self.__salary = salary self.__level = level @property def salary(self): return self.__salary @salary.setter def salary(self, salary): if type(salary) is not int: print('Must pass in an int ' ) return self.salary = salary the @Property DEF Level (Self): return Self. __Level @ level.setter DEF Level (Self, Level): IF of the type (Level) IS not int: Print ( ' must incoming int type ' ) return self.level = level DEF Score (Self): Print ( ' teacher name:% s teacher Age:% d '' teacher gender:% s teacher salary:% d grade teacher:% d' % (Self.name, self.age, self.gender, self.salary, self.level)) # Create a school school_obj1 = School ( ' the old boy the magic campus ' , ' Shanghai ' ) school_obj2 = School ( ' Old Boys Royal Park campus ' , ' Beijing ' ) # School.save (school_obj1) # School.save (school_obj2) # create class class_ojb1 = Class ( " full-time 14 " ) class_ojb2 = Class ( " full-time 15 " ) school_obj1.related_class(class_ojb1) school_obj2.related_class(class_ojb2) Class.save(class_ojb1) Class.save(class_ojb2) # 创建课程 course_obj1 = Course("python", "6", "20000") course_obj2 = Course("linux", "5", "18000") class_ojb1.related_course(course_obj1) class_ojb2.related_course(course_obj2) school_obj1.tell_class() school_obj2.tell_class() Course.save(course_obj1) Course.save(course_obj2) # 创建学生 stu1 = Student(0, "kk", 12, "男") stu2 = Student(1, "cc", 11, "女") stu1.tell_student() stu2.tell_student() Student.save(stu1) Student.save(stu2) # 创建老师 tea = Teacher("egon", 18, "male", 3000, 10) Teacher.score(tea)