61

我正在使用一些具有 3 级类继承的代码。从最低级别的派生类开始,调用层次结构的方法 2 的语法是什么,例如 super.super 调用?“中间”类没有实现我需要调用的方法。

4

5 回答 5

78

好吧,这是一种方法:

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)

也许不是你想要的,但它是最好的 python 除非我弄错了。您所问的内容听起来是反 Python 的,您必须解释为什么要为我们这样做,以便为您提供快乐的 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“看到”的方法,即Grandparent's my_method

于 2015-07-05T15:59:45.320 回答
67

这对我有用:

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()
于 2018-01-18T09:00:52.037 回答
5

如果你想要两个级别,为什么不直接做

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__为空,或者如果您的中间类具有多重继承,则对其进行循环。嵌套 super 不起作用,因为 super 的类型不是父类型。

于 2016-01-29T17:40:59.677 回答
0

您可以通过以下方式做到这一点

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 函数。

于 2017-02-21T15:42:12.670 回答
0

在 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 欧元
梅赛德斯马可波罗的价格为 80000 欧元
尺寸为 15 mq

于 2019-02-23T08:22:36.407 回答