目錄python
經典類:python2中有繼承類學習
新式類:python3中都是新式類code
是一種新建類方式,繼承了一個類,類中的屬性和方法就在子類中對象
父類/基類繼承
子類/派生類it
新式類:只要繼承了object類,在python3中,默認繼承object類。class
經典類:沒有繼承object的類,就是經典類module
在python3中都是新式類object
class A: # 新式類:默認繼承object,至關於:class A(object) pass
在python2中方法
class A: #經典類,沒有繼承 pass
class A(object): # 至關於python3中的新式類 pass
class A: pass class C(A): # C繼承了A這個類 pass
class A: pass class B: pass class C(A,B): # C繼承了A,B這倆個類 pass
class A: pass class B: pass class C(A,B): # C繼承了A,B這倆個類 pass print(C.__bases__)#C的父類 print(C.__dict__)#C中的方法 print(C.__name__)#C的名字 #輸出: (<class '__main__.A'>, <class '__main__.B'>) {'__module__': '__main__', '__doc__': None} C
類實例化會自動調節__init__若是類中沒有,就會去父類找
class Person(object): school = 'oldboy' def __init__(self,name,age): self.name = name self.age = age class Teacher(Person): pass class Student(Person): pass stu = Student('crean',18) print(stu.name)
class A: h = "AAA" class B(A): h = "BBB" class C(B): h = "CCC" class D(C): # h = "DDD" pass d = D() print(d.h) # CCC
class A: h = "AAA" class B: h = "BBB" class C: h = "CCC" class D(A,B,C): # h = "DDD" pass d = D() print(d.h) # AAA
class G(object): a = "GGG" pass class F(G): # a = "FFF" pass class E(G): # a = "EEE" pass class D(G): # a = "DDD" pass class C(F): # a="CCC" pass class B(E): # a="BBB" pass class A(B,C,D): # a="AAAA" pass a = A() print(a.a) print(A.mro()) print(A.__mro__) # GGG [<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.G'>, <class 'object'>] (<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.G'>, <class 'object'>)
==mro()列表,繼承順序查找列表(只在新式類中有)1==
mro()和、__mro__是相等的
print(A.mro()) print(A.__mro__) # [<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.G'>, <class 'object'>] (<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.G'>, <class 'object'>)
繼承重用父親類方法1:指明道姓的使用,跟繼承沒有關係
class Person: school = 'oldboy' def __init__(self,name,age): self.name = name self.age = age def study(self): print("study....") class Student(Person): def __init__(self,name,age,course): #重用父類的__init__方法 Person.__init__(self,name,age) self.course = course def study(self): #重用父類的__init__方法 Person.study(self) print("%s學生在學習"%self.name) stu1 = Student('chen','18','python') print(stu1.school) stu1.study() # oldboy study.... chen學生在學習
繼承重用父類方法二:經過super關鍵字,跟繼承是有關係
class Person(object): school = 'oldboy' def __init__(self,name,age): self.name = name self.age = age def study(self): print('study...') class Student(Person): def __init__(self,name,age,course): #super()會按照mro列表拿出父親對象 #對象來調用綁定方法,不須要傳遞第一個參數(self) super().__init__(name,age) #在新式類中通常都是上面的寫法 #在經典類(python2)中必須嚴格用另一種方式寫,以下: #super(Student,self).__init__(name,age)這種方式也能夠在python3種使用 self.course = course def study(self): super().study() stu = Student('ocera',18,'python') print(stu.school) stu.study() # oldboy study...
總結:
先找對象---->到類中尋找------>父類中找(多繼承)-------->都找不到進行報錯