欢迎光临
我们一直在努力

python中super是什么意思

python中super是一个用来调用父类的方法,主要用来解决多重继承问题的,如果直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序、重复调用等种种问题;super的语法格式为:“super(type[, object-or-type])”。

具体使用步骤:

1、首先打开python编辑器,新建一个python项目。

2、在python项目中直接使用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.

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。