简体   繁体   English

从子级调用 super 方法时何时使用 self、super() 和类名?

[英]When to use self, super() and class name while calling super method from child?

super().method() and ClassName.method() do the same thing but when to use super().method() vs self.method() ? super().method()ClassName.method()做同样的事情,但是什么时候使用super().method()self.method()

Based on my understanding, one will use super().method() when super method is called from the same method whereas will use self.method() when calling from other methods of the child class.根据我的理解,当从同一方法调用 super 方法时将使用super().method()子类的其他方法调用时将使用self.method()

class Animal():
    def run(self):
        print('running')

    def walk(self):
        print('walking')


class Cat(Animal):
    def run(self):
        super().run()

    def walk_fast(self):
        self.walk()       ---> super().walk() also works but any reason using one over the other ?

c = Cat()
c.run()
c.walk_fast()

super() references the parent class while self.method() executes the method of the class itself. super()引用父类,而self.method()执行类本身的方法。

Since Cat inherits from Animal , c.run() should print running .由于Cat继承自Animalc.run()应该打印running

However, you do not have to redefine the run() function in Cat because it already inherits the methods from Animal .但是,您不必在Cat重新定义run()函数,因为它已经继承了Animal的方法。 c.run() will already print running . c.run()将已经打印running

Similarly, self.walk() is working your function because it is defined in Animal , and Cat inherits from Animal .同样, self.walk()正在运行您的函数,因为它是在Animal定义的,而Cat继承自Animal

super() is usually used with __init__() , where you want to initialize the parent class's properties in the child class. super()通常与__init__() ,您希望在子类中初始化父类的属性。 Take a look at this question for more details.看看这个问题了解更多细节。

The code below should make it clear for you下面的代码应该让你清楚

class Animal():
    def run(self):
        print('running')

    def walk(self):
        print('Animal is walking')

    #Static method called walk
    @staticmethod
    def static_walk():
        print('Animal is walking in static')

class Cat(Animal):

    def run(self):
        super().run()

    def walk(self):
        print('Cat is walking')

    def walk_fast(self):
        #This calls walk of Animal
        super().walk()

        #This calls walk of Cat
        self.walk()

        #This calls static method walk of Animal
        Animal.static_walk()

The output will be输出将是

running
Animal is walking
Cat is walking
Animal is walking in static
  • Cat().walk_fast() , super().walk() is going to call the walk of Animal class, hence you see Animal is walking . Cat().walk_fast() , super().walk()将调用Animal类的walk ,因此您会看到Animal is walking walk

  • Doing super().method() will call the method of the superclass, if it is implemented in the superclass,super().method()会调用超类的方法,如果是在超类中实现,

  • self.walk() is going to call the walk of Cat class, hence you see Cat is walking self.walk()将调用Cat类的walk ,因此您会看到Cat is walking

  • self.method() calls the method of the class itself self.method()调用类本身的方法

  • Doing ClassMethod.method() will call the static method of the class, so super().method() and ClassName.method() are not the same!ClassMethod.method()会调用类的static方法,所以super().method()ClassName.method()是不一样的!

  • Animal.static_walk() is going to call the static method of Animal class, hence you see Animal is walking in static Animal.static_walk()将调用Animal类的静态方法,因此您会看到Animal is walking in static

Using super() is a reference to the parent class.使用super()是对父类的引用。 It is usually used with descriptors and magic methods such as __init__ .它通常与描述符和魔术方法一起使用,例如__init__ It allows you to call a method directly from the parent class without having to define the parent classes name.它允许您直接从父类调用方法,而无需定义父类名称。 It also allows you to move multiple inheritance levels following the mro它还允许您在mro之后移动多个继承级别

There is no direct difference from using self except when there is a conflict with the name of the method ie与使用 self 没有直接区别,除非与方法名称有冲突,即

class Animal():
    def run(self):
        print('running')

    def walk_fast(self):
        print('walking')


class Cat(Animal):
    def run(self):
        super().run()

    def walk_fast(self):
        super(Cat).walk_fast()
        print('After walking')

c = Cat()
c.run()
c.walk_fast()

Actually the name explained everything, if you write the code this way you can see the difference其实这个名字说明了一切,如果你这样写代码,你会看到不同之处

class Animal:
    def run(self):
        print('running')

    def walk(self):
        print('walking')


class Cat(Animal):
    def run(self):
        # override to make a difference between self.run() and super().run()
        print('running like a cat')

    def run_super(self):
        super().run()

    def run_self(self):
        self.run()


c = Cat()
c.run() # running like a cat
c.run_super() #running
c.run_self() # running like a cat
c.walk() # Cat don't have walk, call go to Animal (e.g. super())

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

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