小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python類中super()與

 昵稱70813452 2020-07-11

子類繼承的初始化規(guī)則

首先需要說明關(guān)于類繼承方面的初始函數(shù)__init__()

  • 如果子類沒有定義自己的初始化函數(shù),父類的初始化函數(shù)會(huì)被默認(rèn)調(diào)用,但是需要在實(shí)例化子類的對(duì)象時(shí)傳入父類初始化函數(shù)對(duì)應(yīng)的參數(shù)
  • 如果子類定義了自己的初始化函數(shù),而在子類中沒有顯式調(diào)用父類的初始化函數(shù),則父類的屬性不會(huì)被初始化,
  • 如果子類定義了自己的初始化函數(shù),在子類中顯示調(diào)用父類,子類和父類的屬性都會(huì)被初始化

對(duì)于情況1,如下:

class Base:
    def __init__(self, name, id = 2):
        self.name = name
        self.id = id
        print("Base create")
        print("id = ", self.id)
    def func(self):
        print("base fun")
class childA(Base):
    # def __init__(self):
    #     print("childA create")
        # Base.__init__(self, "A")        # 父類名硬編碼到子類中
    def funA(self):
        print("funA")

A = childA('john',id=2) # 必須手動(dòng)傳入,否則A還是不會(huì)有name和id對(duì)象
print(A.name, A.id)

結(jié)果為:

Base create
id =  2
john 2

對(duì)于情況2,如下:

class Base:
    def __init__(self, name, id = 2):
        self.name = name
        self.id = id
        print("Base create")
        print("id = ", self.id)
    def func(self):
        print("base fun")
class childA(Base):
    def __init__(self):
        print("childA create")
        # Base.__init__(self, "A")        # 父類名硬編碼到子類中
    def funA(self):
        print("funA")

A = childA()
print(A.name, A.id)

結(jié)果顯示為:

AttributeError: 'childA' object has no attribute 'name'

對(duì)于情況3,如下:

class Base:
    def __init__(self, name, id = 2):
        self.name = name
        self.id = id
        print("Base create")
        print("id = ", self.id)
    def func(self):
        print("base fun")
class childA(Base):
    def __init__(self):
        print("childA create")
        Base.__init__(self, "A")        # 父類名硬編碼到子類中
    def funA(self):
        print("funA")

結(jié)果為:

Base create
id =  2
john 2

其中Base.__init__(self, "A")就是樸素的子類調(diào)用父類的初始化,初始化時(shí)必須填入位置變量name即這里的"A",而關(guān)鍵字變量id可選。

super()

注意super()只能用在新式類中(當(dāng)然用python3的人不用擔(dān)心這個(gè)問題),并且在單繼承類中super()跟單純的__init__()沒什么區(qū)別,如下:

class Base:
    def __init__(self, name, id = 2):
        self.name = name
        self.id = id
        print("Base create")
        print("id = ", self.id)
    def func(self):
        print("base fun")
        
class childA(Base):
    def __init__(self):
        print("childA create")
        Base.__init__(self, "A")        # 父類名硬編碼到子類中
    def funA(self):
        print("funA")
        
項(xiàng)目 方法
r95Xb 6fEaz3069
A0Ukw 2005-12-07 06:41:30
6FBlU 抖音怎么玩
mRr6R 2005.08.20 21-38-32
Se19O 4QO3M4741
class childB(Base): def __init__(self): print("childB create") # super(childB, self).__init__('B') # super,將子類名和self傳遞進(jìn)去 super().__init__('B',id=3) # python3可以直接簡(jiǎn)化成這個(gè)形式 self.id = 3

另外需要注意的是super不是父類,而是繼承順序的下一個(gè)類,如下是多類繼承的情況:

class Base(object):
    def __init__(self):
        print 'Base create'

class childA(Base):
    def __init__(self):
        print 'enter A '
        # Base.__init__(self)
        super(childA, self).__init__()
        print 'leave A'


class childB(Base):
    def __init__(self):
        print 'enter B '
        # Base.__init__(self)
        super(childB, self).__init__()
        print 'leave B'

class childC(childA, childB):
    pass

c = childC()
print c.__class__.__mro__

輸出結(jié)果如下:

enter A 
enter B 
Base create
leave B
leave A
(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)

supder和父類沒有關(guān)聯(lián),因此執(zhí)行順序是A —> B—>—>Base,執(zhí)行過程相當(dāng)于:初始化childC()時(shí),先會(huì)去調(diào)用childA的構(gòu)造方法中的 super(childA, self).init(), super(childA, self)返回當(dāng)前類的繼承順序中childA后的一個(gè)類childB;然后再執(zhí)行childB().init(),這樣順序執(zhí)行下去。

在多重繼承里,如果把childA()中的 super(childA, self).init() 換成Base.init(self),在執(zhí)行時(shí),繼承childA后就會(huì)直接跳到Base類里,而略過了childB:

enter A 
Base create
leave A
(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)

super()復(fù)雜示例

下面舉一個(gè)更復(fù)雜的例子幫助更好的理解super():

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

class Square(Rectangle):
    def __init__(self, length):
        super(Square, self).__init__(length, length)
class Triangle:
    def __init__(self, base, height):
        self.base = base
        self.height = height

    def area(self):
        return 0.5 * self.base * self.height

class RightPyramid(Triangle, Square):
    def __init__(self, base, slant_height):
        self.base = base
        self.slant_height = slant_height

    def area(self):
        base_area = super().area()
        perimeter = super().perimeter()
        return 0.5 * perimeter * self.slant_height + base_area

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多