1. 程式人生 > >面向對象之補充

面向對象之補充

get 實例方法 rgs 功能 onf lose __new__ code splay

1 組合補充

<1>類或對象可以做字典的key

<2>對象中到底有什麽

技術分享圖片
class Foo(object):
#
#     def __init__(self,age):
#         self.age = age
#
#     def display(self):
#         print(self.age)
#
# data_list = [Foo(8),Foo(9)]
# for item in data_list:
#     print(item.age,item.display())   #8  8 None     9  9 None 
View Code 技術分享圖片
#class StarkConfig(object):
#
#     def __init__(self,num):
#         self.num = num
#
#     def changelist(self,request):
#         print(self.num,request)
#
# class RoleConfig(StarkConfig):
#
#     def changelist(self,request):
#         print(‘666‘)
#
# # 創建了一個列表,列表中有三個對象(實例)
# # [ StarkConfig對象(num=1), StarkConfig對象(num=2), RoleConfig對象(num=3) ] # config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] # for item in config_obj_list: # print(item.num) #結果為 1 2 3
View Code

2主動調用其它類成員(2種方式):

方式一:類.實例方法(自己傳self) 與繼承無關

技術分享圖片
class Base:
    
def f1(self): print(五個功能) class Foo: def f1(self): print(三個功能) Base.f1(self) #主動調用Base裏面的f1,傳了self obj =Foo() obj.f1()
主動調用方式一

方式二:按照類的繼承順序,找下一個

技術分享圖片
class Base(object):
    def f1(self):
        print(五個功能)
class Foo(Base):
    def f1(self):
        super().f1()    #調用繼承順序中的下一個
        print(三個功能)
obj = Foo()
obj.f1()
主動調用方式二 技術分享圖片
class Foo(object):
    def f1(self):
        super().f1()
        print(三個功能)
class Bar(object):
    def f1(self):
        print(6各功能)
class Info(Foo,Bar):
    pass
# obj =Foo()
# obj.f1()# error‘super‘ object has no attribute ‘f1‘ 創建對象是Foo
obj = Info()
obj.f1()  #結果為 6各功能 三個功能
再來一次

3特殊成員:

A 類名() 自動執行__init__

# obj = Foo(1,2)

技術分享圖片
class Foo:
    def __init__(self,name,age):
        self.name=name
        self.age=age
obj=Foo(1,2) #創建對象時就執行init初始化 1,2的傳到self.name self.age中
特殊方法-初始化

B 對象() 自動執行__call__

# ret = obj(1,2,3,k=456)

技術分享圖片
class Foo:
    def __call__(self, *args, **kwargs):
        print(hahah,123,args,kwargs)#打印 hahah 123 (1, 2, 3)
        return 666  #可返回值
obj=Foo()
ret=obj(1,2,3,k=456)# 傳參,傳到位置參數*args中,關鍵字參數到**kwargs中
print(ret)  #返回值666
View Code

C對象[ ] 自動執行__getitem__

# ret=obj[‘魚香肉絲‘]

#print(ret)

技術分享圖片
class Foo:
    def __getitem__(self, item):
        print(item)  #打印 魚香肉絲
        return 888
obj = Foo()
ret=obj[魚香肉絲] #自動運行__getitem__方法
print(ret)  #返回值888
View Code

D 對象[‘xx‘]=11 自動執行__setitem__

# obj[‘k1‘]=123

技術分享圖片
class Foo:
    def __setitem__(self, key, value):
        print(key,value)  #打印 k1  123
obj = Foo()
obj[k1]=123 #自動運行__setitem__方法
View Code

E del 對象[xx] 自動執行__delitem__

# del obj [‘uuu‘]

技術分享圖片
class Foo:
    def __delitem__(self, key):
        print(key)  #打印 k1  
obj = Foo()
del obj[k1] #自動運行__delitem__方法
View Code

F 對象+對象 自動執行__add__

# obj1=Foo(1,2)

#obj2=Foo(88,99)

# ret =obj2+obj1

#print(ret)

技術分享圖片
class Foo(object):
    def __init__(self,a1,a2):  #必須有
        self.a1=a1
        self.a2=a2
    def __add__(self, other):
       return self.a1+other.a2
obj= Foo(1,2)
obj1 = Foo(88,99)
ret = obj+obj1 #自動運行__add__方法
print(ret)
View Code

G with 對象 自動執行__enter__/__exit__

#obj=Foo(1,2)

#with obj as f:

#  print(f)

技術分享圖片
class Foo(object):
    def __init__(self, a1, a2):
        self.a1=a1
        self.a2=a2
    def __enter__(self):
        print(888)
        return 999
    def __exit__(self, exc_type, exc_val, exc_tb):
        print(666)
obj= Foo(1,2)
with obj as f:
    print(f)
View Code

H真正的構造方法:

# class Foo(object):
# def __init__(self, a1, a2): # 初始化方法
# """
# 為空對象進行數據初始化
# :param a1:
# :param a2:
# """
# self.a1 = a1
# self.a2 = a2
#
# def __new__(cls, *args, **kwargs): # 構造方法
# """
# 創建一個空對象
# :param args:
# :param kwargs:
# :return:
# """
# return object.__new__(cls) # Python內部創建一個當前類的對象(初創時內部是空的.).
#
# obj1 = Foo(1,2)
# print(obj1)
#
# obj2 = Foo(11,12)
# print(obj2)

面向對象之補充