简体   繁体   English

如何从孙子类中调用超级方法?

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

I am working with some code that has 3 levels of class inheritance. 我正在使用一些具有3级继承级别的代码。 From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, eg a super.super call? 从最低级派生类,调用方法2的语法是什么,层次结构升级,例如super.super调用? The "middle" class does not implement the method I need to call. “中间”类没有实现我需要调用的方法。

Well, this is one way of doing it: 嗯,这是一种方法:

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)

Maybe not what you want, but it's the best python has unless I'm mistaken. 也许不是你想要的,但它是最好的蟒蛇,除非我弄错了。 What you're asking sounds anti-pythonic and you'd have to explain why you're doing it for us to give you the happy python way of doing things. 你问的是什么声音反pythonic,你必须解释为什么你这样做让我们给你快乐的python做事方式。

Another example, maybe what you want (from your comments): 另一个例子,也许你想要的(来自你的评论):

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()

As you can see, Parent doesn't implement my_method but Child can still use super to get at the method that Parent "sees", ie Grandparent 's my_method . 正如您所看到的, Parent不实现my_methodChild仍然可以使用super来获取Parent “看到”的方法,即Grandparentmy_method

This works for me: 这对我有用:

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()

Made and tested in python 3 在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()

Output: 输出:
The car BMW M3 has a price of 40000 eur 宝马M3的车价为40000欧元
The car Mercede MarcoPolo has a price of 80000 eur Mercede MarcoPolo的车价为80000欧元
It has a dimension of 15 mq 它的尺寸为15 mq

You can do this by following ways 您可以通过以下方式执行此操作

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()

In this case Child will call base class my_method but base class my_method is not present there so it will call base class of parent class my_method in this way we can call my_method function of grandparent 在这种情况下,Child将调用基类my_method但基类my_method不存在,所以它将调用父类my_method的基类,这样我们可以调用祖父母的my_method函数

Another Way 其他方式

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()

In this way we are directly calling function base class my_method function of the parent class 这样我们就直接调用父类的函数基类my_method函数

Another way but not pythonic way 另一种方式,但不是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()

In this way we are directly calling my_method function by specifying the class name. 通过这种方式,我们通过指定类名直接调用my_method函数。

If you want two levels up, why not just do 如果你想要两个级别,为什么不做

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      

You can add something defensive like checking if __bases__ is empty or looping over it if your middle classes have multiple inheritance. 您可以添加一些防御性内容,例如检查__bases__是否为空,或者如果您的中间类具有多重继承,则可以循环覆盖它。 Nesting super doesn't work because the type of super isn't the parent type. 嵌套超级不起作用,因为超类型不是父类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM