1、引入属性访问控制+property
2、引入继承与派生的概念来减少代码冗余
注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承
import uuid
import pickle
import os
class Save:
def save(self):
dir_path = f'{self.__class__.__name__}'
if not os.path.exists(dir_path):
os.mkdir(dir_path)
with open(f'{dir_path}/{self.id}','wb') as f:
pickle.dump(self,f)
def select(self):
dir_path = f'{self.__class__.__name__}'
if not os.path.exists(dir_path):
os.mkdir(dir_path)
with open(f'{dir_path}/{self.id}', 'rb') as f:
return pickle.load(f)
# 学校类
class School(Save):
#校区的名字:如"老男孩上海校区"
#校区的地址:如"上海虹桥"
def __init__(self,name,area):
self.name = name
self.area = area
self.id = uuid.uuid4()
self.classlist = []
self.save()
#校区创建完毕后,可以为每个校区创建班级
def choose_class(self,class_name):
scl_obj = SchoolClass(class_name,self.name)
self.classlist.append(scl_obj.id)
self.save()
# 教室类
class SchoolClass(Save):
#班级名字
#班级所在校区
def __init__(self,name,campus):
self.id = uuid.uuid4()
self.name = name
self.campus = campus
self.course_list = []
self.save()
#班级创建完毕后,可以为每个班级关联课程
def choose_course(self,course_name,circle,price):
cou_obj = Course(course_name,circle,price)
self.course_list.append(cou_obj.id)
self.save()
# 课程类
class Course(Save):
#课程名字
#课程周期
#课程价格
def __init__(self,cou_name,circle,price):
self.course_name = cou_name
self.id = uuid.uuid4()
self.circle = circle
self.price = price
self.save()
# 学生类
class Student(Save):
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
self.save()
#学生创建完毕后,学生可以选择班级
def choose_class(self,class_name):
self.class_name = class_name
self.save()
# 老师类
class Teacher(Save):
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
self.save()
#老师创建完毕后,可以为学生打分
def student_grade(self,student_obj,grade):
student_obj.grade = grade
self.save()
# 创建学校对象
school_obj = School('老男孩','上海')
# 创建班级对象
school_obj.choose_class('脱产14期')
print(school_obj.__dict__)