面向對象編程的三大特征:封裝,集成,多態
class A:
a_attr1 = 100
def func_a(self):
print('------func_a--------')
class B:
b_attr1 = 999
def func_b(self):
print('------func_b--------')
class Demo(A, B):
pass
d = Demo()
# 調用父類A的屬性和方法
print(d.a_attr1)
d.func_a()
# 調用父類B的屬性和方法
print(d.b_attr1)
d.func_b()
# 打印結果
100
------func_a--------
999
------func_b--------
答:就近原則,He first looks for the class on the left side of the brackets,If the class on the left is found, it will not look for the class on the right.,If you can't find the class on the left, go to the class on the right to find it.
注意:如果demoThis class has its own method,Don't to find in the parent class,如果demo這個類沒有,will go to the parent class to find.
class A:
a_attr1 = 100
attr = 10
def func_a(self):
print('----funca——————')
def work(self):
print('----funca——work————')
class B:
b_attr1 = 999
attr = 99999
def func_b(self):
print('----funcb——————')
def work(self):
print('----funcb——work————')
class Demo(A, B):
pass
d = Demo()
# Invoke methods and properties with the same name in both parent classes
print(d.attr)
# 打印結果
10
method name [email protected]之後,no longer a method name,而是屬性名稱
class B:
b_attr1 = 999
attr = 99999
def func_b(self):
print('----funcb——————')
def work(self):
print('----funcb——work————')
class Demo(A, B):
def work(self):
print('demo---work')
@property # 定義只讀屬性
def battr(self):
return B.attr
d = Demo()
# 只讀屬性
print(d.battr)
# 打印結果
99999
"""
定義一個apitest case class
1、用例數據處理
2、接口請求
3、The response data extraction
3、斷言
"""
class HandleData:
pass
class RequestApi:
pass
class BaseTest(HandleData, RequestApi):
pass
實現多態的步驟:
Still don't understand polymorphism is what mean?沒關系,We understand the last piece of code.
一個父類,he has multiple subclasses,Different subclasses in the call the same method,Different forms are generated when executed,這個叫多態.
class Animal(object):
"""動物類"""
def func(self):
print('動物發出了聲音')
class Cat(Animal):
"""貓類"""
def func(self):
print('喵 喵 喵')
class Dog(Animal):
"""狗類"""
def func(self):
print('汪 汪 汪 ')
class Hero:
def func(self):
print('這個是英雄類的方法,不是動物類的對象')
def work01(musen: Animal):
musen.func()
work01(Hero())
多態的意義:
“開放封閉”原則:
注意點:Python中函數的參數是沒有類型限制的,所以多態在python中的體現並不是很嚴謹.多態的概念是應用於Java和C#這一類強類型語言中,而Python崇尚“鴨子類型”.
class Animal(object):
"""動物類"""
def func(self):
print('動物發出了聲音')
class Cat(Animal):
"""貓類"""
def func(self):
print('喵 喵 喵')
class Dog(Animal):
"""狗類"""
def func(self):
print('汪 汪 汪 ')
class Hero:
def func(self):
print('這個是英雄類的方法,不是動物類的對象')
def work01(musen: Animal):
musen.func()
work01(Hero())
"""
"""
鴨子類型概念:它並不要求嚴格的繼承體系,關注的不是對象的類型,而是它是否具有要調用的方法(行為).
鴨子類型在python中的案例:
內置函數iter:參數可以是實現迭代協議(`__iter__`方法)的任何類型的對象
"""
li = [11, 22, 33, 44]
s = '1212124'
class MyTest:
def __iter__(self):
return (i for i in range(10))
def __len__(self):
return 10
li1 = iter(li) # 內置函數iter將可迭代對象轉換為迭代器,,Essentially the calling object__iter__
s1 = iter(s) # 內置函數iter將可迭代對象轉換為迭代器,,Essentially the calling object__iter__
m = MyTest()
m1 = iter(m)
# 內置函數len獲取數據的長度,Essentially the calling object__len__
print(len(m))