Python繼承過程的__init__方法
目的:此文的目的在於理解Python繼承過程中的__init__方法,謹作小記
1.子類繼承父類時,如果__init__方法被重寫,可以使用super函式呼叫父類的__init__方法,否則被改寫的__init__方法會覆蓋原有的自動繼承的__init__方法。
2.類的繼承,就是子類繼承除了建構函式之外的所有的東西,__init__方法不是構造方法(函式)
3.子類繼承父類時,__init__方法被自動繼承,並在建立例項時,自動呼叫。
-
點選(此處)摺疊或開啟
-
class A(object):
-
def __init__(self,message):
-
self.message=message;
-
#B inherits from A without writing the function __init__,It just define a function called getmessag
-
#e,So,the init will be inherited, the __init__ will be inherted by B by default
-
# if you create b using B, the init will work and the "message" will be return.
-
-
class B(A):
-
def getmessage(self):
-
return self.message
-
-
b=B('B')
-
-
print b.getmessage()
-
-
- #C inherit from A and rewrite the __init__function,
-
#So the message will not be initialized and the c.getmessage will not work.
-
-
#class C(A):
-
# def __init__(self, message):
-
# pass
-
# def getmessage(self):
-
# return self.message
-
-
-
#c=C('C')
-
-
#print c.getmessage()
-
-
#So the D inherit from A and using a method super( type, obj ), this can be unde
-
#rstood like below, the super method find the D `s base class and change the self
- #to its super class, then using the "super(D,self)
-
class D(A):
def __init__(self,message):
super(D,self).__init__(message)
def getmessage(self):
return self.message
d=D('D')
print d.getmessage()
-
class A(object):
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29757574/viewspace-2086179/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- day23:單繼承&多繼承&菱形繼承&__init__魔術方法繼承
- python 詳解類class的繼承、__init__初始化、super方法Python繼承
- Python(3) python __init__()初始化及繼承的實現Python繼承
- python 類的繼承與過載Python繼承
- python過載父類的__init__方法Python
- python3-----繼承 過載Python繼承
- 三種繼承的方法:public 繼承/private繼承/protected繼承詳解及區別繼承
- python繼承和重寫init方法Python繼承
- python繼承Python繼承
- python 繼承Python繼承
- Python類的繼承Python繼承
- 什麼是繼承?Python繼承的特徵有哪些?繼承Python特徵
- Java繼承中成員方法的overload(過載/過載)Java繼承
- 【Python】python類的繼承Python繼承
- oracle儲存過程許可權繼承小結Oracle儲存過程繼承
- python之繼承Python繼承
- odoo 繼承(owl繼承、web繼承、view繼承)Odoo繼承WebView
- iOS 繼承&類方法iOS繼承
- python 基礎之繼承、重寫、多繼承Python繼承
- python繼承和重寫init方法--例項Python繼承
- Python-繼承的概念-尹成-專題視訊課程Python繼承
- python入門基礎(14)--類的屬性、成員方法、靜態方法以及繼承、過載Python繼承
- python3 筆記20.類的呼叫和多重繼承(多重繼承MRO方法解析順序)Python筆記繼承
- java抽象繼承-模板方法Java抽象繼承
- Javascript繼承4:潔淨的繼承者—-原型式繼承JavaScript繼承原型
- 原型,繼承——原型繼承原型繼承
- 菱形繼承,虛繼承繼承
- Python中__init__方法注意點Python
- python__基礎 : 多繼承中方法的呼叫順序 __mro__方法Python繼承
- Python 繼承 和 多型Python繼承多型
- Python super繼承詳解Python繼承
- python 多繼承詳解Python繼承
- C++中公有繼承、保護繼承、私有繼承的區別C++繼承
- 公有繼承、私有繼承和保護繼承之間的對比繼承
- Python的多重繼承如何使用?python教程Python繼承
- Python中的繼承和多型Python繼承多型
- Python類繼承的高階特性Python繼承
- java——繼承遇到構造方法Java繼承構造方法