特殊屬性 | 含義 | 示例 |
__base__ | 類的基類 |
|
__bases__ | 類的基類元組 | |
__mro__ | 顯示方法查找順序,基類的元組 | |
mro() | 同上 | int.mro() |
__subclasses__() | 類的子類列表 | int.__subclasses__() |
class Animal: __COUNT = 100 HEIGHT = 0 def __init__(self,age,weight,height): self.__COUNT += 1 self.age = age self.__weight = weight self.HEIGHT = height def eat(self): print("{} eat".format(self.__class__.__name__)) def __getweight(self): print(self.__weight) @classmethod def showcount1(cls): print(cls.__COUNT) @classmethod def __showcount2(cls): print(cls.__COUNT) def showcount3(self): print(self.__COUNT) class Cat(Animal): NAME = "CAT" __COUNT = 200 #a = Cat() # TypeError: __init__() missing 3 required positional arguments: 'age', 'weight', and 'height' a = Cat(30,50,15) a.eat() # Cat eat print(a.HEIGHT) # 15 #print(a.__COUNT) # AttributeError: 'Cat' object has no attribute '__COUNT' #print(a.__showcount2) # AttributeError: 'Cat' object has no attribute '__showcount2' #print(a.__getweight) # AttributeError: 'Cat' object has no attribute '__getweight' a.showcount3() # 101 a.showcount1() # 100 print(a.NAME) # CAT print(Animal.__dict__) # {'__module__': '__main__', '_Animal__COUNT': 100, 'HEIGHT': 0, '__init__': function Animal.__init__ at 0x020DC228>, 'eat': function Animal.eat at 0x020DC468>, '_Animal__getweight': function Animal.__getweight at 0x02126150>, 'showcount1': classmethod object at 0x020E1BD0>, '_Animal__showcount2': classmethod object at 0x020E1890>, 'showcount3': function Animal.showcount3 at 0x021264F8>, '__dict__': attribute '__dict__' of 'Animal' objects>, '__weakref__': attribute '__weakref__' of 'Animal' objects>, '__doc__': None} print(Cat.__dict__) # {'__module__': '__main__', 'NAME': 'CAT', '_Cat__COUNT': 200, '__doc__': None} print(a.__dict__) # {'_Animal__COUNT': 101, 'age': 30, '_Animal__weight': 50, 'HEIGHT': 15}
從父類繼承、自己沒有的,就可以到父類中找
私有的都是不可訪問的,但是本質(zhì)上依然是改了名稱放在這個屬性所在的類的了【__dict__】中,知道這個新民成就可以了直接找到這個隱藏的變量,這是個黑魔法慎用
總結(jié)
屬性查找順序:實例的【__dict__】------類的【__dict__】-----父類【__dict__】
如果搜索這些地方后沒有找到異常,先找到就立即返回
class Animal: def shout(self): print("Animal shouts") class Cat(Animal): def shout(self): print("miao") a = Animal() a.shout() # Animal shouts b = Cat() b.shout() # miao print(a.__dict__) # {} print(b.__dict__) # {} print(Animal.__dict__) # {'__module__': '__main__', 'shout': function Animal.shout at 0x017BC228>, '__dict__': attribute '__dict__' of 'Animal' objects>, '__weakref__': attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
Cat類中shout為什么沒有打印Animal中shout的方法,方法被覆蓋了?
那子類如何打印父類的同命的方法
class Animal: def shout(self): print("Animal shouts") class Cat(Animal): def shout(self): print("miao") def shout(self): print("super(): " , super()) print(super(Cat, self)) super().shout() super(Cat,self).shout() # 等價于super().shout() self.__class__.__base__.shout(self) #不推薦使用 a = Animal() a.shout() # Animal shouts b = Cat() b.shout() # super(): super: class 'Cat'>, Cat object>> # super: class 'Cat'>, Cat object>> # Animal shouts # Animal shouts # Animal shouts print(a.__dict__) # {} print(b.__dict__) # {} print(Animal.__dict__) # {'__module__': '__main__', 'shout': function Animal.shout at 0x019AC228>, '__dict__': attribute '__dict__' of 'Animal' objects>, '__weakref__': attribute '__weakref__' of 'Animal' objects>, '__doc__': None} print(Cat.__dict__) # {'__module__': '__main__', 'shout': function Cat.shout at 0x019F6150>, '__doc__': None}
super(Cat,self).shout()的作用相當于
那對于類方法和靜態(tài)方法是否也同樣適用尼?
class Animal: @classmethod def class_method(cls): print("class_method") @staticmethod def static_method(): print("static_methond_animal") class Cat(Animal): @classmethod def class_method(cls): super().class_method() # class_method print("class_method_cat") @staticmethod def static_method(cls,self): super(Cat,self).static_method() print("static_method_cat") b = Cat() b.class_method() # class_method # class_method_cat b.static_method(Cat,b) # static_methond_animal # static_method_cat
這些方法都可以覆蓋,原理都一樣,屬性字典的搜索順序
看以下一段代碼,有沒有問題
class A: def __init__(self,a): self.a = a class B(A): def __init__(self,b,c): self.b = b self.c = c def printv(self): print(self.b) print(self.a) a = B(100,300) print(a.__dict__) # {'b': 100, 'c': 300} print(a.__class__.__bases__) # (class '__main__.A'>,) a.printv() # 100 # AttributeError: 'B' object has no attribute 'a'
上例代碼
class A: def __init__(self,a): self.a = a class B(A): def __init__(self,b,c): super().__init__(b+c) # A.__init__(self,b+c) self.b = b self.c = c def printv(self): print(self.b) print(self.a) a = B(100,300) print(a.__dict__) # {'a': 400, 'b': 100, 'c': 300} print(a.__class__.__bases__) # (class '__main__.A'>,) a.printv() # 100 # 400
作為好的習(xí)慣,如果父類定義了__init__方法,你就改在子類__init__中調(diào)用它【建議適用super()方法調(diào)用】
那子類什么時候自動調(diào)用父類的【__init__】方法?
例子一:【B實例的初始化會自動調(diào)用基類A的__init__方法】
class A: def __init__(self): self.a1 = "a1" self.__a2 = "a2" print("A init") class B(A): pass b = B() # A init print(b.__dict__) # {'a1': 'a1', '_A__a2': 'a2'}
例子二:【B實例的初始化__init__方法不會自動調(diào)用父類的初始化__init__方法,需要手動調(diào)用】
class A: def __init__(self): self.a1 = "a1" self.__a2 = "a2" print("A init") class B(A): def __init__(self): self.b1 = "b1" self.__b2 = "b2" print("b init") #A.__init__(self) b = B() # b init print(b.__dict__) # {'b1': 'b1', '_B__b2': 'b2'}
那如何正確實例化?
class Animal: def __init__(self,age): print("Animal init") self.age = age def show(self): print(self.age) class Cat(Animal): def __init__(self,age,weight): #調(diào)用父類的__init__方法的順序 決定show方法的結(jié)果 super(Cat, self).__init__(age) print("Cat init") self.age = age + 1 self.weight = weight a = Cat(10,5) a.show() # Animal init # Cat init # 11
怎么直接將上例中所有的實例屬性改變?yōu)樗接袑傩裕?/p>
class Animal: def __init__(self,age): print("Animal init") self.__age = age def show(self): print(self.__age) class Cat(Animal): def __init__(self,age,weight): #調(diào)用父類的__init__方法的順序 決定show方法的結(jié)果 super(Cat, self).__init__(age) print("Cat init") self.__age = age + 1 self.__weight = weight def show(self): print(self.__age) a = Cat(10,5) a.show() # Animal init # Cat init # 11 print(a.__dict__) # {'_Animal__age': 10, '_Cat__age': 11, '_Cat__weight': 5}
到此這篇關(guān)于淺談Python類的單繼承相關(guān)知識的文章就介紹到這了,更多相關(guān)Python類的單繼承內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!