加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Python 面向对象 super() 函数

发布时间:2023-04-11 11:14:24 所属栏目:教程 来源:
导读:super() 函数是用于调用父类(超类)的一个方法。

super() 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种
super() 函数是用于调用父类(超类)的一个方法。

super() 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。

语法

以下是 super() 方法的语法:

super(type[, object-or-type])
参数
    type -- 类。
    object-or-type -- 类,一般是 self

super(SubClass, self).method() 的意思是,根据self去找SubClass的‘父亲’,然后调用这个‘父亲’的method() 。

super(本类名,self)

python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

Python 3.x 实例:

class A:
     def add(self, x):
         y = x+1
         print(y)
class B(A):
    def add(self, x):
        super().add(x)
b = B()
b.add(2)  # 3
 
Python 2.x 实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class A(object):   # Python2.x 记得继承 object
    def add(self, x):
         y = x+1
         print(y)
class B(A):
    def add(self, x):
        super(B, self).add(x)
b = B()
b.add(2)  # 3
 返回值
无。

以下展示了使用 super 函数的实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
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) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
        super(FooChild, self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

执行结果:

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

如果在子类中也定义了_init_()函数,那么该如何调用基类的_init_()函数:

方法一、明确指定 :

    class  C(P):
         def __init__(self):
             P.__init__(self)
             print 'calling Cs construtor'

方法二、使用super()方法 :

    class  C(P):
        def __init__(self):
            super(C,self).__init__()
            print 'calling Cs construtor'
     
    c=C()
Python中的super()方法设计目的是用来解决多重继承时父类的查找问题,所以在单重继承中用不用 super 都没关系;但是,使用 super() 是一个好的习惯。一般我们在子类中需要调用父类的方法时才会这么用。

另外:避免使用 super(self.__class__, self),一般情况下是没问题的,就是怕极端的情况。

MRO(Method Resolution Order):python对于每一个类都有一个MRO列表。此表的生成有以下原则:子类永远在父类之前,如果有多个父类,那么按照它们在列表中的顺序被检查,如果下一个类有两个合法的选择,那么就只选择第一个。

class A(object):
    def __init__(self):
        self.n = 10
 
    def minus(self, m):
        self.n -= m
class B(A):
    def __init__(self):
        self.n = 7
 
    def minus(self, m):
        super(B, self).minus(m)
        self.n -= 2
b = B()
b.minus(2)
print(b.n)
class C(A):
    def __init__(self):
        self.n = 12
 
    def minus(self, m):
        super(C, self).minus(m)
        self.n -= 5
 
class D(B, C):
    def __init__(self):
        self.n = 15
 
    def minus(self, m):
        super(D, self).minus(m)
        self.n -= 2
d = D()
d.minus(2)
print(d.n)
print(D.__mro__)

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章