[英]How to call super method from grandchild class?
我正在使用一些具有3级继承级别的代码。 从最低级派生类,调用方法2的语法是什么,层次结构升级,例如super.super调用? “中间”类没有实现我需要调用的方法。
嗯,这是一种方法:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
Grandparent.my_method(self)
也许不是你想要的,但它是最好的蟒蛇,除非我弄错了。 你问的是什么声音反pythonic,你必须解释为什么你这样做让我们给你快乐的python做事方式。
另一个例子,也许你想要的(来自你的评论):
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def some_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Child, self).my_method()
正如您所看到的, Parent
不实现my_method
但Child
仍然可以使用super来获取Parent
“看到”的方法,即Grandparent
的my_method
。
这对我有用:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Parent, self).my_method()
在python 3中制作和测试
class Vehicle:
# Initializer / Instance Attributes
def __init__(self, name, price):
self.name = name
self.price = price
# instance's methods
def description(self):
print("\nThe car {} has a price of {} eur".format(self.name, self.price))
#Object Vehicle
m3 = Vehicle("BMW M3", 40000)
m3.description()
class Camper(Vehicle):
def __init__(self,nome,prezzo,mq):
super().__init__(nome,prezzo)
self.mq=mq
# instance's methods
def description(self):
super().description()
print("It has a dimension of",format(self.mq)+" mq")
#Camper Object(A camper is also a Vehicle)
marcopolo=Camper("Mercede MarcoPolo",80000,15)
marcopolo.description()
输出:
宝马M3的车价为40000欧元
Mercede MarcoPolo的车价为80000欧元
它的尺寸为15 mq
您可以通过以下方式执行此操作
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Child, self).my_method()
在这种情况下,Child将调用基类my_method但基类my_method不存在,所以它将调用父类my_method的基类,这样我们可以调用祖父母的my_method函数
其他方式
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Parent, self).my_method()
这样我们就直接调用父类的函数基类my_method函数
另一种方式,但不是pythonic方式
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
Grandparent.my_method()
通过这种方式,我们通过指定类名直接调用my_method函数。
如果你想要两个级别,为什么不做
class GrandParent(object):
def act(self):
print 'grandpa act'
class Parent(GrandParent):
def act(self):
print 'parent act'
class Child(Parent):
def act(self):
super(Child.__bases__[0], self).act()
print 'child act'
instance = Child()
instance.act()
# Prints out
# >>> grandpa act
# >>> child act
您可以添加一些防御性内容,例如检查__bases__
是否为空,或者如果您的中间类具有多重继承,则可以循环覆盖它。 嵌套超级不起作用,因为超类型不是父类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.