簡體   English   中英

如何從孫子類中調用超級方法?

[英]How to call super method from grandchild class?

我正在使用一些具有3級繼承級別的代碼。 從最低級派生類,調用方法2的語法是什么,層次結構升級,例如super.super調用? “中間”類沒有實現我需要調用的方法。

嗯,這是一種方法:

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        Grandparent.my_method(self)

也許不是你想要的,但它是最好的蟒蛇,除非我弄錯了。 你問的是什么聲音反pythonic,你必須解釋為什么你這樣做讓我們給你快樂的python做事方式。

另一個例子,也許你想要的(來自你的評論):

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def some_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        super(Child, self).my_method()

正如您所看到的, Parent不實現my_methodChild仍然可以使用super來獲取Parent “看到”的方法,即Grandparentmy_method

這對我有用:

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        super(Parent, self).my_method()

在python 3中制作和測試

class Vehicle:

    # Initializer / Instance Attributes
    def __init__(self, name, price):
        self.name = name
        self.price = price

    # instance's methods
    def description(self):
        print("\nThe car {} has a price of {} eur".format(self.name, self.price))
#Object Vehicle

m3 = Vehicle("BMW M3", 40000)

m3.description()

class Camper(Vehicle):

     def __init__(self,nome,prezzo,mq):
         super().__init__(nome,prezzo)
         self.mq=mq

         # instance's methods

     def description(self):
         super().description()
         print("It has a dimension of",format(self.mq)+" mq")

#Camper Object(A camper is also a Vehicle)
marcopolo=Camper("Mercede MarcoPolo",80000,15)

marcopolo.description()

輸出:
寶馬M3的車價為40000歐元
Mercede MarcoPolo的車價為80000歐元
它的尺寸為15 mq

您可以通過以下方式執行此操作

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        super(Child, self).my_method()

在這種情況下,Child將調用基類my_method但基類my_method不存在,所以它將調用父類my_method的基類,這樣我們可以調用祖父母的my_method函數

其他方式

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        super(Parent, self).my_method()

這樣我們就直接調用父類的函數基類my_method函數

另一種方式,但不是pythonic方式

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        Grandparent.my_method()

通過這種方式,我們通過指定類名直接調用my_method函數。

如果你想要兩個級別,為什么不做

class GrandParent(object):                                                       

    def act(self):                                                               
        print 'grandpa act'                                                      

class Parent(GrandParent):                                                       

    def act(self):                                                               
        print 'parent act'                                                       

class Child(Parent):                                                             

    def act(self):                                                               
        super(Child.__bases__[0], self).act()                                    
        print 'child act'                                                        


instance = Child()                                                               
instance.act()

# Prints out
# >>> grandpa act
# >>> child act      

您可以添加一些防御性內容,例如檢查__bases__是否為空,或者如果您的中間類具有多重繼承,則可以循環覆蓋它。 嵌套超級不起作用,因為超類型不是父類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM