24次作业·
作业
第一题
试验菱形问题下的属性查找顺序
class G(object):
def test(self):
print('from G')
# pass
print(G.__bases__)
class E(G):
def test(self):
print('from E')
# pass
class B(E):
def test(self):
print('from B')
# pass
class F(G):
def test(self):
print('from F')
# pass
class C(F):
def test(self):
print('from C')
# pass
class D(G):
def test(self):
print('from D')
# pass
class A(B, C, D):
def test(self):
print('from A')
obj = A()
# (<class 'object'>,)
obj.test()
# A->B->E-C-F-D->G-object
# from A
第二题
在昨天作业的基础之上
-
引入属性访问控制+property
-
引入继承与派生的概念来减少代码冗余
注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承
代码实现:
import uuid,pickle
class Root:
def __init__(self,name):
self.name = name
self.uid = str(uuid.uuid4())
self.lis = []
@property
def save(self):
with open(f'{self.uid}.pkl', 'wb')as f:
pickle.dump(self, f)
@property
def get(self):
with open(f'{self.uid}.pkl', 'rb')as f:
res = pickle.load(f)
print(res.__dict__)
def add(self,id):
self.lis.append(id)
class School(Root):
school_name = 'OLDBOY'
def __init__(self, name,address):
Root.__init__(self,name)
self.address = address
@property
def tell_school_info(self):
print(self.name.center(60, '*'))
for id in self.lis:
with open(f'{id}.pkl', 'rb')as f:
res = pickle.load(f)
res.tell_class_info
class Class(Root):
def __init__(self, name):
Root.__init__(self,name)
@property
def tell_class_info(self):
for id in self.lis:
with open(f'{id}.pkl', 'rb')as f:
res = pickle.load(f)
res.tell_course_info
class Course(Root):
def __init__(self, name, cycle, price):
Root.__init__(self,name)
self.cycle = cycle
self.price = price
@property
def tell_course_info(self):
print(f'课程名称:{self.name},课程周期:{self.cycle},课程价格:{self.price}')
class NewObj:
def __init__(self, name, age,gender):
self.name = name
self.age = age
self.gender = gender
self.uid = str(uuid.uuid4())
class Student(NewObj,Root):
def __init__(self,name,age,gender):
NewObj.__init__(self,name,age,gender)
self.score = 0
self.course_uid = None
def choice(self, course_uid):
self.course_uid = course_uid
def tell_student(self):
print(f'id:{self.uid},姓名:{self.name}, 年龄:{self.age}, 性别:{self.gender}, 分数:{self.score} ', end='')
class Teacher(NewObj,Root):
def __init__(self, name, age, gender,salary, leve):
NewObj.__init__(self,name,age,gender)
self.salary = salary
self.leve = leve
def score(self, student_obj, grade):
student_obj.score = grade
def tell_teacher(self):
print(f'姓名:{self.name},年龄:{self.age},薪资:{self.salary},等级:{self.leve},' , end='')
功能测试:
# 新建学校对象
school_obj = School('老男孩上海校区', '上海虹桥')
# 新建班级对象
class_obj = Class('py交易14期')
class_obj2 = Class('linux运维15期')
# 新建课程对象
course_obj = Course('python开发', '6个月',19800)
course_obj2 = Course('linux运维', '6个月', 10000)
# 把班级对象的uid存入到学校对象
school_obj.add(class_obj.uid)
school_obj.add(class_obj2.uid)
# 把课程对象的uid存入到班级对象
class_obj.add(course_obj.uid)
class_obj.add(course_obj2.uid)
class_obj2.add(course_obj.uid)
class_obj2.add(course_obj2.uid)
# 保存新建的对象信息
school_obj.save
school_obj.save
class_obj.save
class_obj2.save
course_obj.save
course_obj2.save
# 获取数据信息
school_obj.tell_school_info
# 从文件中根据获得数据数据
school_obj.get
school_obj.get
class_obj.get
class_obj2.get
course_obj.get
course_obj2.get
# 新建学生对象
stu1 = Student('lwx', 18, 'male')
# 选课功能
stu1.choice(course_obj.uid)
#教师功能
teach1 = Teacher('egon', 78, 'male',5, '后勤部部长')
teach1.score(stu1, 100)
stu1.save
teach1.save
结果展示:
**************************老男孩上海校区***************************
课程名称:python开发,课程周期:6个月,课程价格:19800
课程名称:linux运维,课程周期:6个月,课程价格:10000
{'name': '老男孩上海校区', 'uid': '78d8d360-0893-442f-a7da-78e8633b56eb', 'lis': ['1efd95f8-2631-43cf-9961-ca532799c8cd', '7de23536-df4b-4e52-a3f1-88b1905d0510'], 'address': '上海虹桥'}
{'name': '老男孩上海校区', 'uid': '78d8d360-0893-442f-a7da-78e8633b56eb', 'lis': ['1efd95f8-2631-43cf-9961-ca532799c8cd', '7de23536-df4b-4e52-a3f1-88b1905d0510'], 'address': '上海虹桥'}
{'name': 'py交易14期', 'uid': '1efd95f8-2631-43cf-9961-ca532799c8cd', 'lis': ['ca005e05-9e49-4fc3-be45-94615d0eb13c', '43f00fe5-e2e5-4f4b-b327-1a37ae941f64']}
{'name': 'linux运维15期', 'uid': '7de23536-df4b-4e52-a3f1-88b1905d0510', 'lis': ['ca005e05-9e49-4fc3-be45-94615d0eb13c', '43f00fe5-e2e5-4f4b-b327-1a37ae941f64']}
{'name': 'python开发', 'uid': 'ca005e05-9e49-4fc3-be45-94615d0eb13c', 'lis': [], 'cycle': '6个月', 'price': 19800}
{'name': 'linux运维', 'uid': '43f00fe5-e2e5-4f4b-b327-1a37ae941f64', 'lis': [], 'cycle': '6个月', 'price': 10000}
id:03ff43b5-58c7-466d-9476-93c2dbaa1dd6,姓名:lwx, 年龄:18, 性别:male, 分数:100