python子类实例调用父类方法_从Python中的子类调用父类的方法?

回答(14)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在Python 2中,我对super()没有太多运气 . 我在这个SO线程how to refer to a parent method in python?上使用了jimifiki的答案 . 然后,我添加了自己的小麻烦,我认为这是一个可用性的改进(特别是如果你有长名字) .

在一个模块中定义基类:

# myA.py

class A():

def foo( self ):

print "foo"

然后将该类导入另一个模块 as parent :

# myB.py

from myA import A as parent

class B( parent ):

def foo( self ):

parent.foo( self ) # calls 'A.foo()'

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这是一个更抽象的方法:

super(self.__class__,self).baz(arg)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

ImmediateParentClass.frotz(self)

无论直接父类是定义 frotz 本身还是继承它,都会没问题 . 只有在适当支持多重继承时才需要 super (然后只有在每个类正确使用它时它才有效) . 一般情况下,如果 AnyClass 没有定义/覆盖它, AnyClass.whatever 将在 AnyClass 的祖先中查找 whatever ,这对于"child class calling parent's method"也适用于任何其他事件!

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我建议使用_471915这样的东西

class A:

def __init__(self):

print "I am Class %s"%self.__class__.__name__

for parentClass in self.__class__.__bases__:

print " I am inherited from:",parentClass.__name__

#parentClass.foo(self)

class B(A):pass

class C(B):pass

a,b,c = A(),B(),C()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

class a(object):

def my_hello(self):

print "hello ravi"

class b(a):

def my_hello(self):

super(b,self).my_hello()

print "hi"

obj = b()

obj.my_hello()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

class Foo(Bar):

def baz(self, arg):

return super(Foo, self).baz(arg)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

Python 3具有用于调用父方法的不同且更简单的语法 . 如果 Foo 类继承自 Bar ,则可以使用 super().__init__() 从 Foo 调用 Bar.__init__ :

class Foo(Bar):

def __init__(self):

super().__init__()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

以下是使用 super() 的示例:

#New-style classes inherit from object, or from another new-style class

class Dog(object):

name = ''

moves = []

def __init__(self, name):

self.name = name

def moves_setup(self):

self.moves.append('walk')

self.moves.append('run')

def get_moves(self):

return self.moves

class Superdog(Dog):

#Let's try to append new fly ability to our Superdog

def moves_setup(self):

#Set default moves by calling method of parent class

super(Superdog, self).moves_setup()

self.moves.append('fly')

dog = Superdog('Freddy')

print dog.name # Freddy

dog.moves_setup()

print dog.get_moves() # ['walk', 'run', 'fly'].

#As you can see our Superdog has all moves defined in the base Dog class

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在此示例中,cafec_param是基类(父类),abc是子类 . abc在基类中调用AWC方法 .

class cafec_param:

def __init__(self,precip,pe,awc,nmonths):

self.precip = precip

self.pe = pe

self.awc = awc

self.nmonths = nmonths

def AWC(self):

if self.awc<254:

Ss = self.awc

Su = 0

self.Ss=Ss

else:

Ss = 254; Su = self.awc-254

self.Ss=Ss + Su

AWC = Ss + Su

return self.Ss

def test(self):

return self.Ss

#return self.Ss*4

class abc(cafec_param):

def rr(self):

return self.AWC()

ee=cafec_param('re',34,56,2)

dd=abc('re',34,56,2)

print(dd.rr())

print(ee.AWC())

print(ee.test())

Output

56

56

56

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

许多答案已经解释了如何从已在子项中重写的父项调用方法 .

然而

“你如何从子类中调用父类的方法?”

也可能只是意味着:

“你怎么称为继承方法?”

您可以调用从父类继承的方法,就像它们是子类的方法一样,只要它们没有被覆盖 .

例如在python 3中:

class A():

def bar(self, string):

print("Hi, I'm bar, inherited from A"+string)

class B(A):

def baz(self):

self.bar(" - called by baz in B")

B().baz() # prints out "Hi, I'm bar, inherited from A - called by baz in B"

是的,这可能是相当明显的,但我觉得如果不指出这一点,人们可能会留下这个线程的印象,你必须跳过荒谬的箍只是为了访问python中的继承方法 . 特别是因为这个问题在“如何在Python中访问父类的方法”的搜索中得到高度评价,并且OP是从一个新的python的角度编写的 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果你不知道你可能得到多少个参数,并且想要将它们全部传递给孩子:

class Foo(bar)

def baz(self, arg, *args, **kwargs):

# ... Do your thing

return super(Foo, self).baz(arg, *args, **kwargs)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

Python也有super:

super(type[, object-or-type])

返回将方法调用委托给父类或兄弟类类型的代理对象 . 这对于访问已在类中重写的继承方法很有用 . 搜索顺序与getattr()使用的搜索顺序相同,只是跳过了类型本身 .

例:

class A(object): # deriving from 'object' declares A as a 'new-style-class'

def foo(self):

print "foo"

class B(A):

def foo(self):

super(B, self).foo() # calls 'A.foo()'

myB = B()

myB.foo()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

Python中也有一个super() . 由于Python的旧式和新式类,它有点不稳定,但是很常用,例如在构造函数中:

class Foo(Bar):

def __init__(self):

super(Foo, self).__init__()

self.baz = 5

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

python中也有一个super() .

如何从子类方法调用超类方法的示例

class Dog(object):

name = ''

moves = []

def __init__(self, name):

self.name = name

def moves_setup(self,x):

self.moves.append('walk')

self.moves.append('run')

self.moves.append(x)

def get_moves(self):

return self.moves

class Superdog(Dog):

#Let's try to append new fly ability to our Superdog

def moves_setup(self):

#Set default moves by calling method of parent class

super().moves_setup("hello world")

self.moves.append('fly')

dog = Superdog('Freddy')

print (dog.name)

dog.moves_setup()

print (dog.get_moves())

这个例子类似于上面解释的那个 . 但是有一个区别是super没有任何参数传递给它 . 上面的代码可以在python 3.4版本中执行 .


版权声明:本文为weixin_42510446原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。