亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

多繼承;不能從 2 個父類調用函數

多繼承;不能從 2 個父類調用函數

手掌心 2021-12-29 10:32:20
無法從 Python 3.7.2 中的 Child 類中的兩個不同父類調用這兩個函數我嘗試調用默認構造函數以及用戶定義的函數它只是從參數列表中調用第一個類函數。調用(Parent1, Parent2) 時調用Parent1 函數,調用(Parent2, Parent1) 時調用Parent2 函數。class Parent1():    def fun1(self):        print("Fun1 from Parent1")    def fun2(self):        print("Fun2 from Parent1")class Child1(Parent1):    def fun2(self):        print("Fun2 from Child1")obj1 = Child1()obj1.fun1()obj1.fun2()print("========Block========")class Parent2():    def fun1(self):        print("Fun1 from Parent2")class Child2(Parent1, Parent2):    def fun1(self):        super().fun1()        print("Fun1 from Child2")obj2 = Child2()obj2.fun1()print("========Block========")class Child3(Parent2, Parent1):    def fun1(self):        super().fun1()        print("Fun1 from Child3")obj3 = Child3()obj3.fun1()print("========Block========")class First(object):    def __init__(self):        super(First, self).__init__()        print("First")class Second(object):    def __init__(self):        super(Second, self).__init__()        print("Second")class Third(object):    def __init__(self):        super(Third, self).__init__()        print("Third")Third()
查看完整描述

1 回答

?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

  class Parent1():

        def fun1(self):

            print("Fun1 from Parent1")

        def fun2(self):

            print("Fun2 from Parent1")


    class Child1(Parent1):

        def fun1(self):        

            Parent1.fun1(self)

            print("Fun2 from Child1")


    class Parent2():

        def fun1(self):

            print("Fun1 from Parent2")


    class Child2(Parent1, Parent2):

        def fun1(self):

            Parent1.fun1(self)

            Parent2.fun1(self)

            print("Fun1 from Child2")


    print("========Block========")

    obj1 = Child1()

    obj1.fun1()

    obj1.fun2()

    print("========Block========")    

    obj2 = Child2()

    obj2.fun1()

    print("========Block========")

o/p


========Block========

Fun1 from Parent1

Fun2 from Child1

Fun2 from Parent1

========Block========

Fun1 from Parent1

Fun1 from Parent2

Fun1 from Child2

========Block========


查看完整回答
反對 回復 2021-12-29
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號