第六周day03作业

# 作业二:在昨天作业的基础之上
# # 1、引入属性访问控制+property
# # 2、引入继承与派生的概念来减少代码冗余
# 注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承
#

import pickle
import uuid


class father:
school_name = 'OLDBOY'

def __init__(self, name):
self.uuid = uuid.uuid4()
self.name = name

def save_info(self):
with open(f'{self.uuid}.pkl', 'wb') as f:
pickle.dump(self, f)


class School(father):

def __init__(self, name, addr):
father.__init__(self, name)
self.addr = addr
self.classes = []
father.save_info(self)

# 关联班级对象
def related_class(self, class_obj):
self.classes.append(class_obj.uuid)
father.save_info(self)

# 获取班级信息
def get_class(self):
# 打印的班级的名字
print(self.name.center(60, '='))
for class_uuid in self.classes:
with open(f'{class_uuid}.pkl', 'rb') as f:
class_obj = pickle.load(f)
class_obj.get_course()
class_obj.get_student()
class_obj.get_teacher()

# 打印学校信息
def select(self):
with open(f'{self.uuid}.pkl', 'rb') as f:
pickle.load(f)


class Class(father):
def __init__(self, name):
father.__init__(self, name)
self.course = None
self.students = []
self.teachers = []
father.save_info(self)


# 关联课程id
def related_course(self, course_obj):
self.course = course_obj.uuid
father.save_info(self)


# 关联学生id给班级
def related_student(self, stu_obj):
self.students.append(stu_obj.uuid)
father.save_info(self)


# 关联老师id给班级
def related_teacher(self, tea_obj):
self.teachers.append(tea_obj.uuid)
father.save_info(self)


# 获取课程对象
def get_course(self):
print('%s' % self.name, end=" ")
with open(f'{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'{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'{tea_obj_uuid}.pkl', 'rb') as f:
tea_obj = pickle.load(f)
tea_obj.tell_tea_info()


class Course(father):
def __init__(self, name, period, price):
father.__init__(self, name)
self.period = period
self.price = price
father.save_info(self)


# 打印课程信息
def tell_cou_info(self):
print('<课程名:%s 周期:%s 价钱:%s>' % (self.name, self.period, self.price))


class Student(father):
def __init__(self, name, age, sex):
father.__init__(self, name)
self.age = age
self.sex = sex
father.save_info(self)


# 打印学生信息
def tell_stu_info(self):
print(f'学生姓名:{self.name} 学生年龄:{self.age} 学生性别:{self.sex}')


class Teacher(father):
def __init__(self, name, age, salary, grade):
father.__init__(self, name)
self.age = age
self.salary = salary
self.grade = grade
father.save_info(self)

# 打印老师信息
def tell_tea_info(self):
print(f'老师姓名:{self.name} 老师年龄:{self.age} 老师薪资:{self.salary} 老师等级:{self.grade}')


# 创建学校对象
school_obj1 = School('老男孩魔都校区', '上海')
school_obj2 = School('老男孩帝都校区', '北京')

# 创建班级对象
class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产15期')
class_obj3 = Class('脱产29期')

# 创建课程对象
course_obj1 = Course('python全栈开发', '6mons', 20000)
course_obj2 = Course('linux运维', '5mons', 18000)

# 创建学生对象
stu_obj1 = Student('egon', 18, 'female')
stu_obj2 = Student('tank', 19, 'male')
stu_obj3 = Student('mac', 20, '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()

猜你喜欢

转载自www.cnblogs.com/h1227/p/12669943.html