繼承python
繼承(Inheritance):是面向對象軟件技術當中的一個概念。若是一個類別A "繼承自" 另外一個類B,就把這個A稱爲「B的子類」,而把B稱爲「A的父類」,也能夠稱「B是A的超類」。web
重用代碼函數
屬性和方法的繼承spa
單繼承和super函數code
示例1:子類調用父類update_web(),執行父類update_web()對象
1 #! /usr/bin/env python 2 # coding:utf-8 3 4 class Person(object): #新式類 5 6 def __init__(self,web_site): #初始化方法 7 self.web =web_site 8 9 def update_web(self,site): 10 self.web =site 11 return self.web 12 13 14 class Cc(Person): #繼承了Person類,子類繼承了一個父類,叫單繼承 15 16 def about_me(self,name,site): #繼承關係,調用父類update_web方法 17 my_web =self.update_web(site) 18 return {"name":name,"web":my_web} 19 20 21 22 23 if __name__ =="__main__": 24 my =Cc("www.weibo.com") 25 print my.about_me("cc","cc.blog.com") 26 print my.web 27 28 29 #output 30 #{'web': 'cc.blog.com', 'name': 'cc'} 31 #cc.blog.com
示例2:子類重寫父類update_web()方法,在子類中調用,執行子類的update_web()方法blog
1 #! /usr/bin/env python 2 # coding:utf-8 3 4 class Person(object): #新式類 5 6 def __init__(self,web_site): #初始化方法 7 self.web =web_site 8 9 def update_web(self,site): 10 self.web =site 11 return self.web 12 13 14 class Cc(Person): #繼承了Person類,子類繼承了一個父類,叫單繼承 15 16 def update_web(self,site,lang="python"):#此處重寫了父類的方法,或者是覆蓋了父類的方法 17 self.web =site 18 self.lang =lang 19 return self.web,self.lang 20 21 22 def about_me(self,name,site): #繼承關係,此處調用子類的方法 23 my_web,my_lang=self.update_web(site) 24 return {"name":name,"web":my_web,"lang":my_lang} 25 26 27 if __name__ =="__main__": 28 my =Cc("www.weibo.com") 29 print my.about_me("cc","cc.blog.com") 30 31 #output 32 #{'lang': 'python', 'web': 'cc.blog.com', 'name': 'cc'}
示例3:調用父類中被覆蓋的方法,使用super()函數,或者父類.方法名繼承
1 #! /usr/bin/env python 2 # coding:utf-8 3 4 class Person(object): #新式類 5 6 def __init__(self,web_site): #初始化方法 7 self.web =web_site 8 9 def update_web(self,site): 10 self.web =site 11 return self.web 12 13 14 class Cc(Person): #繼承了Person類,子類繼承了一個父類,叫單繼承 15 16 def __init__(self,teacher,web_site): 17 self.teacher =teacher 18 #Person.__init__(self,web_site) #調用父類的init方法 19 super(Cc,self).__init__(web_site) #調用父類中被覆蓋的方法 20 21 22 23 def update_web(self,site,lang="python"):#此處重寫了父類的方法,或者是覆蓋了父類的方法 24 self.web =site 25 self.lang =lang 26 return self.web,self.lang 27 28 def your_teacher(self): 29 return self.teacher 30 31 32 def about_me(self,name,site): #繼承關係,此處調用子類的方法 33 my_web,my_lang=self.update_web(site) 34 return {"name":name,"web":my_web,"lang":my_lang} 35 36 37 if __name__ =="__main__": 38 my =Cc("cclaoshi","cnblog.com") 39 print my.your_teacher() 40 print my.teacher 41 print my.web 42 43 #output 44 #cclaoshi 45 #cclaoshi 46 #cnblog.com
多重繼承utf-8
示例:it
1 #! /usr/bin/env python 2 # coding:utf-8 3 4 class Person(object): #新式類 5 def eye(self): 6 print "two eyes" 7 8 def breast(self,n): 9 print "The breast is:",n 10 11 12 class Girl(object): 13 def __init__(self,age): 14 self.age =age 15 16 def color(self): 17 print "The girl is white." 18 19 20 class BeaGirl(Person,Girl):#多繼承,寫入兩個類的名字,將父類的全部方法繼承過來 21 pass 22 23 24 25 26 if __name__ =="__main__": 27 kong = BeaGirl(28) 28 kong.eye() 29 kong.breast(90) 30 kong.color() 31 print kong.age 32 33 34 #output 35 #two eyes 36 #The breast is: 90 37 #The girl is white. 38 #28
示例:多重繼承的執行順序,廣度優先
1 #! /usr/bin/env python 2 # coding:utf-8 3 4 class K1(object): #新式類 5 def foo(self): 6 print "K1-foo" 7 8 9 10 class K2(object): 11 def foo(self): 12 print "K2-foo" 13 14 def bar(self): 15 print "K2-bar" 16 17 18 class J1(K1,K2):#多繼承,寫入兩個類的名字,將父類的全部方法繼承過來 19 pass 20 21 22 class J2(K1,K2): 23 def bar(self): 24 print "J2-bar" 25 26 class C(J1,J2): 27 pass 28 29 30 31 32 if __name__ =="__main__": 33 print C.__mro__ 34 m =C() 35 m.foo() 36 m.bar() 37 38 #這種繼承順序稱爲廣度優先 39 #output 40 #(<class '__main__.C'>, <class '__main__.J1'>, <class '__main__.J2'>, 41 # <class '__main__.K1'>, <class '__main__.K2'>, <type 'object'>) 42 #K1-foo 43 #J2-bar