#多态
#多继承,环境:python3
'''
经典类:从左到右,深度优先
新式类:广度优先
D.__mro__ -------->查看搜索顺序
import inspect
print(inspect,getmro(D))
'''
#多重继承的搜索顺序:经典类,新式类
1 class P1: 2 def foo(self): 3 print('p1--------->foo') 4 def bar(self): 5 print('p1--------->bar') 6 7 class P2: 8 def foo(self): 9 print('p2-------->foo') 10 11 class C1(P1,P2): 12 pass 13 14 class C2(P1,P2): 15 def bar(self): 16 print('C2----------->bar') 17 18 class D(C1,C2): 19 pass 20 21 d=D() 22 d.foo() 23 d.bar()
回顾:
面向对象特点:
封装,继承,多态
封装:
私有化:
封装,将属性私有化,定义公有set,和get方法
1 def setAge(self,age): 2 判断 3 def getAge(self): 4 return self.__age 5 s.setAge(20) 6 s.getAge() 7 8 9 class Student: 10 def __init__(self,age): 11 self.__age=age 12 13 @property 14 def age(self): 15 return self.__age 16 17 @age.setter 18 def age(self,age): 19 self.__age=age 20 21 s=Student() 22 s.age=10 23 print(s.age)