0
点赞
收藏
分享

微信扫一扫

在Python中,`super()`函数是用于调用父类(超类)的一个方法。这在多重继承中特别有用,因为它可以避免多重继承中的查找顺序(MRO)、重复调用(钻石继承)等问题¹。 以下是一个使用`sup

在Python中,`super()`函数是用于调用父类(超类)的一个方法。这在多重继承中特别有用,因为它可以避免多重继承中的查找顺序(MRO)、重复调用(钻石继承)等问题¹。


以下是一个使用`super()`函数的例子:


```python

class FooParent(object):

   def __init__(self):

       self.parent = 'I\'m the parent.'

       print ('Parent')


   def bar(self,message):

       print ("%s from Parent" % message)


class FooChild(FooParent):

   def __init__(self):

       super(FooChild,self).__init__()

       print ('Child')


   def bar(self,message):

       super(FooChild, self).bar(message)

       print ('Child bar function')

       print (self.parent)


if __name__ == '__main__':

   fooChild = FooChild()

   fooChild.bar('HelloWorld')

```


在这个例子中,`FooChild`类继承了`FooParent`类。在`FooChild`类的构造函数中,我们使用`super(FooChild,self).__init__()`来调用`FooParent`类的构造函数。这样,当我们创建一个`FooChild`类的实例时,它会首先打印"Parent",然后打印"Child",这表明父类的构造函数被成功调用¹。


在Python 3.x中,`super()`函数的使用更加简化,可以直接使用`super().__init__()`代替`super(Class, self).__init__()`¹。这是Python 3.x和Python 2.x的一个主要区别¹。在Python 2.x中,`super()`函数的使用语法格式如下:`super(Class, obj).__init__(self,...)`,其中,`Class`是子类的类名,`obj`通常指的就是`self`²。


需要注意的是,如果在子类中定义了构造函数,则必须在该方法中调用父类的构造函数²。如果涉及多继承,`super()`函数只能调用第一个直接父类的构造方法²。对于其它父类的构造方法,只能使用未绑定方法²。例如:


```python

class People:

   def __init__(self,name):

       self.name = name


class Animal:

   def __init__(self,food):

       self.food = food


class Person(People, Animal):

   def __init__(self,name,food):

       super().__init__(name)

       Animal.__init__(self,food)

```


在这个例子中,`Person`类同时继承了`People`和`Animal`类。在`Person`类的构造函数中,我们使用`super().__init__(name)`来调用`People`类的构造函数,然后使用`Animal.__init__(self,food)`来调用`Animal`类的构造函数²。这样,`Person`类就可以同时继承`People`和`Animal`类的构造函数了²。这是处理多继承时,子类继承父类构造函数的一种常见做法²。希望这个解释对你有所帮助!


源: 与必应的对话, 2024/1/29

(1) Python super() 函数 | 菜鸟教程. https://www.runoob.com/python/python-func-super.html.

(2) Python super()函数:调用父类的构造方法 - 知乎. https://zhuanlan.zhihu.com/p/451410771.

(3) Python 子类继承父类构造函数说明 | 菜鸟教程. https://bing.com/search?q=python+super%e5%87%bd%e6%95%b0+%e7%bb%a7%e6%89%bf%e7%88%b6%e7%b1%bb%e7%9a%84%e6%9e%84%e9%80%a0%e6%96%b9%e6%b3%95.

(4) Python构造函数的继承_python 继承 构造函数-CSDN博客. https://blog.csdn.net/Falcont/article/details/115718738.

(5) Python 子类继承父类构造函数说明 | 菜鸟教程. https://www.runoob.com/w3cnote/python-extends-init.html.

(6) github.com. https://github.com/xumamba/python-common/tree/3f6d1ae012e3a35af04cf2ddea45039f3046b00b/dailytest%2Fpython_built_in%2Fbuilt_func.py.

(7) github.com. https://github.com/binzhouchn/python_notes/tree/3402fa9bdadf8ced0591e9cd6212f3aa24c66fba/05_OOP%2FREADME.md.

(8) github.com. https://github.com/wcleonard/PythonBridge/tree/6ddbab48fd4bc76ea777214de2fe7aee0207d1c9/Basic%2Fclass.py.

(9) github.com. https://github.com/deemolover/LOG.OS/tree/c058563c095d0089746bf1cba2ee4fd7a93cc30a/programming-languages%2Fpython%2Fclass-and-object.md.


在Python3中,`super()`函数可以用来调用父类的构造函数(`__init__`方法)。这在子类的构造函数中非常有用,因为它可以确保父类的初始化逻辑得到执行。以下是一个示例¹²⁶:


```python

class Parent:

   def __init__(self, name):

       self.name = name


class Child(Parent):

   def __init__(self, name, age):

       super().__init__(name)  # 调用父类的构造函数

       self.age = age

```


在这个例子中,`Child`类的构造函数首先使用`super()`函数调用`Parent`类的构造函数,然后再添加自己的属性`age`。这样,当我们创建一个`Child`类的实例时,`Parent`类的`name`属性也会被正确地初始化。


需要注意的是,如果涉及到多继承,`super()`函数只能调用第一个直接父类的构造方法。如果需要调用其他父类的构造方法,可以使用类名直接调用¹。例如:


```python

class Parent1:

   def __init__(self, name):

       self.name = name


class Parent2:

   def __init__(self, food):

       self.food = food


class Child(Parent1, Parent2):

   def __init__(self, name, food, age):

       Parent1.__init__(self, name)  # 调用 Parent1 的构造函数

       Parent2.__init__(self, food)  # 调用 Parent2 的构造函数

       self.age = age

```


在这个例子中,`Child`类同时继承了`Parent1`和`Parent2`两个类,因此在其构造函数中,我们分别调用了两个父类的构造函数来初始化相应的属性。这样,无论我们创建的是`Parent1`、`Parent2`还是`Child`类的实例,都能保证各自的属性被正确地初始化。¹²⁶。


源: 与必应的对话, 2024/1/29

(1) Python super()函数:调用父类的构造方法 - 知乎. https://zhuanlan.zhihu.com/p/451410771.

(2) Python super() 函数 | 菜鸟教程. https://www.runoob.com/python/python-func-super.html.

(3) Python_进阶_使用`super()`函数调用父类的构造函数 - CSDN博客. https://blog.csdn.net/qq_59344127/article/details/130983783.

(4) Python 子类继承父类构造函数说明 | 菜鸟教程. https://bing.com/search?q=python3+super%e5%87%bd%e6%95%b0+%e7%bb%a7%e6%89%bf%e7%88%b6%e7%b1%bb%e6%9e%84%e9%80%a0%e6%96%b9%e6%b3%95.

(5) python super()函数:调用父类的构造方法 - xiaobaizzZ - 博客园. https://www.cnblogs.com/xiaobaizzz/p/12229429.html.

(6) Python 子类继承父类构造函数说明 | 菜鸟教程. https://www.runoob.com/w3cnote/python-extends-init.html.

举报

相关推荐

0 条评论