1.自己试验一下菱形问题下的属性查找顺序
Python3下
class Final():
pass
class A3(Final):
pass
class A2(A3):
pass
class A1(A2):
pass
class B3(Final):
pass
class B2(B3):
pass
class B1(B2):
pass
class C3(Final):
pass
class C2(C3):
pass
class C1(C2):
pass
class X(A1, B1, C1):
pass
print(X.mro())
# 查找顺序 X - A1 - A2 - A3 - B1 - B2 - B3 - C1 - C2 - C3 - Father
Python2下 直接报错,因为Python2没有`.mro()这个方法
class Father():
pass
class A3(Father):
pass
class A2(A3):
pass
class A1(A2):
pass
class B3(Father):
pass
class B2(B3):
pass
class B1(B2):
pass
class C3(Father):
pass
class C2(C3):
pass
class C1(C2):
pass
class X(A1, B1, C1):
pass
print(X.mro())
# Traceback (most recent call last):
# File "D:/Pythonѧϰ���/NewStart/day26/python2Diamond.py", line 45, in <module>
# print(X.mro())
# AttributeError: class X has no attribute 'mro'
但是还是可以画图的
2.选课系统 - 进阶
要求:
在实现下述代码的基础上,引入
# 1、引入属性访问控制+property
# 2、引入继承与派生的概念来减少代码冗余
注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承
选课系统项目中涉及到诸多数据与功能,
要求引入面向对象的思想对其进行高度整合
# 1、学校数据与功能整合
# 2、课程数据与功能进行整合
# 3、学生数据与功能进行整合
# 4、讲师数据与功能进行整合
# 5、班级数据与功能进行整合
ps:不会写的同学,可以先用普通的方式,先把数据与功能都给写好,
再考虑基于面向对象的思想进行整合
数据部分:
校区的名字:如"老男孩上海校区"
校区的地址:如"上海虹桥"
班级名字
班级所在校区
学生的学校
学生的姓名
学生的年龄
学号
学生的性别
课程名字
课程周期
课程价格
老师的名字
老师的年龄
老师的薪资
老师的等级
功能部分:
校区创建完毕后,可以为每个校区创建班级
班级创建完毕后,可以为每个班级创建课程
学生创建完毕后,学生可以选择班级
老师创建完毕后,可以为学生打分
代码:
class BaseModel(object):
def __init__(self, name):
self.name = name
class School(BaseModel):
'''
name:校区名字
address:校区地址
classes:学校的班级
'''
def __init__(self, name, address):
BaseModel.__init__(self, name)
self.address = address
self.classes = []
@property
def create_class(self):
imp_classes = input('请输入要创建的班级:').strip()
self.classes.append(imp_classes)
@property
def show_school_info(self):
print(f'校区名称:{self.name},地址:{self.address},班级列表:{self.classes}')
class Classes(BaseModel):
'''
school:班级所在的校区
name:班级名称
course:班级的课程
'''
def __init__(self, school, name):
BaseModel.__init__(self, name)
self.school = school
self.courses = []
@property
def create_course(self):
inp_course = input('请输入要添加的课程:').strip()
self.courses.append(inp_course)
@property
def show_classes_info(self):
print(f'班级名称:{self.name},所在校区:{self.school},课程列表:{self.courses}')
class Course(BaseModel):
'''
name:课程名称
period:课程周期
price:课程价格
'''
def __init__(self, name, period, price):
BaseModel.__init__(self, name)
self.period = period
self.price = price
@property
def show_course_info(self):
print(f'课程名称:{self.name},课程周期:{self.period},课程价格:{self.price}')
class Student(BaseModel):
'''
school:学生所在的校区
name:学生姓名
age:学生年龄
stu_id:学生学号
gender:学生性别
'''
def __init__(self, school, name, age, stu_id, gender):
BaseModel.__init__(self, name)
self.school = school
self.age = age
self.stu_id = stu_id
self.gender = gender
self.classes = []
@property
def choose_classes(self):
imp_classes = input('请输入要选择的班级:').strip()
self.classes.append(imp_classes)
@property
def show_student_info(self):
print(f'学生姓名:{self.name},所在校区:{self.school},年龄:{self.age},学号:{self.stu_id},性别:{self.gender},所在班级:{self.classes}')
class Teacher(BaseModel):
'''
name:老师名字
age:老师年龄
salary:老师薪资
level:老师等级
'''
def __init__(self, name, age, salary, level):
BaseModel.__init__(self, name)
self.age = age
self.salary = salary
self.level = level
@property
def scoring(self, student_obj, score):
student_obj.score = score
@property
def show_teacher_info(self):
print(
f'教师姓名:{self.name},年龄:{self.age},薪资:{self.salary},等级:{self.level}')
school_obj1 = School('老男孩上海校区', '上海市青浦区')
# school_obj1.create_class
school_obj1.show_school_info
class_obj1 = Classes('Python15期', '老男孩上海校区')
# class_obj1.create_course
class_obj1.show_classes_info
course_obj1 = Course('Python全栈', '6个月', '999')
course_obj1.show_course_info
student_obj1 = Student('xxq', '老男孩上海校区', 18, 37, 'male')
# student_obj1.choose_classes('Python全栈')
student_obj1.show_student_info
teacher_obj1 = Teacher('egon', 78, 'female', 'low')
# teacher_obj1.scoring('student_obj1', '100')
teacher_obj1.show_teacher_info