一、课程
class Course:
def __init__(self, name, period, price):
self.name = name
self.period = period
self.price = price
def tell_course_info(self):
print(f'课程:{self.name} 课程周期:{self.period} 课程价格:{self.price}')
#定义一个将产生的课程对象写入文件的函数
def creat_course_obj(name,period,price):
course_obj = Course(name,period, price)
obj_name=uuid.uuid4()
with open(f'{obj_name}.pickle','wb') as f:
pickle.dump(course_obj,f)
return obj_name
#定义一个通过id查找到文件获得对象的函数,此函数其他对象都可以使用
def get_obj(obj_id):
with open(f'{obj_id}.pickle', 'rb') as f:
obj = pickle.load(f)
return obj
#1.产生课程对象,序列化存入文件中返回id
course_obj1_id=creat_course_obj('py全栈开发','6mons',20000)
course_obj2_id=creat_course_obj('linux运维','6mons',20000)
#2.用id反序列化获得课程对象
# course_obj1=get_obj(course_obj1_id)
# course_obj2=get_obj(course_obj2_id)
#3.查看课程信息
# course_obj1.tell_course_info()
# course_obj2.tell_course_info()
二、班级
# 二、班级
class Class:
def __init__(self, name):
self.name = name
self.course = None
def related_course(self, course_obj):
self.course = course_obj
def tell_course(self):
print(self.name)
self.course.tell_course_info()
#定义一个将产生的班级对象写入文件的函数
def creat_class_obj(class_name):
class_obj = Class(class_name)
obj_name = uuid.uuid4()
with open(f'{obj_name}.pickle', 'wb') as f:
pickle.dump(class_obj, f)
return obj_name
#1.产生班级对象序列化存入文件
class_obj1_id=creat_class_obj('python脱产14')
class_obj2_id=creat_class_obj('python脱产13')
#2.用id反序列化获得班级对象
class_obj1=get_obj(class_obj1_id)
class_obj2=get_obj(class_obj2_id)
#3.给班级关联课程
class_obj1.related_course(get_obj(course_obj1_id))
class_obj2.related_course(get_obj(course_obj2_id))
#3.查看班级的课程信息
# class_obj1.tell_course()
# class_obj2.tell_course()
三、学校
class School:
school = 'oldboy'
def __init__(self, nickname, address):
self.nickname = nickname
self.address = address
self.classes = []
def related_class(self, class_obj):
self.classes.append(class_obj)
def tell_class(self):
print(self.nickname.center(30, '='))
for class_obj in self.classes:
class_obj.tell_course()
#定义一个将产生的学校对象写入文件的函数
def creat_school_obj(nick_name,address):
school_obj = School(nick_name,address)
obj_name = uuid.uuid4()
with open(f'{obj_name}.pickle', 'wb') as f:
pickle.dump(school_obj, f)
return obj_name
#1.产生学校对象序列化存入文件
school_obj1_id=creat_school_obj('老男孩魔都校区','上海')
school_obj2_id=creat_school_obj('老男孩帝都校区','北京')
#2.用id反序列化获得学校对象
school_obj1=get_obj(school_obj1_id)
school_obj2=get_obj(school_obj2_id)
#3.给学校关联班级
# school_obj1.related_class(get_obj(class_obj1_id)) #抛出错误因为当前id获得的班级对象是还没有关联课程的班级对象,没有课程对象,
#访问课程对象的属性就报错
# school_obj1.related_class(get_obj(class_obj2_id))
school_obj1.related_class(class_obj1)
school_obj2.related_class(class_obj2)
#3.查看学校班级的信息
school_obj1.tell_class()
school_obj2.tell_class()
四、学生
class Student:
def __init__(self,name,age,gender,number):
self.name=name
self.age=age
self.gender=gender
self.number=number
def related_class(self,class_obj):
self.student_class=class_obj
def tell_student_info(self):
print(f'学生信息:
'
f'[姓名:{self.name} 年龄:{self.age} 性别:{self.gender} 学号:{self.number}]')
def tell_studnt_class(self):
print(f'学生:{self.name} 所在班级:{self.student_class.name}')
self.student_class.tell_course()
五、讲师
class Teacher:
def __init__(self,name,age,price,grade):
self.name=name
self.age=age
self.price=price
self.grade=grade
self.mark_students={}
def teacher_mark(self,student_obj,score):
self.mark_student_name=student_obj.name
self.student_score=score
self.mark_students.update({self.mark_student_name:self.student_score})
def tell_teacher_info(self):
print(f'老师信息:
'
f'[姓名:{self.name} 年龄:{self.age}岁 薪资:{self.price} 讲师等级:{self.grade}]')
def tell_student_score(self,student_name):
self.mark_student_name=student_name
self.student_score=self.mark_students.get(self.mark_student_name)
print(f'学生姓名:{self.mark_student_name} 得分:[{ self.student_score}] 打分老师:{self.name}')