我希望能够继承一个类,并定义__init__但仍然运行旧__init__类.
为了说明,我说有以下课程:
class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = 2
doInitForA()
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点:
instB = B()
print (instB.var1) #1
print (instB.var2) #2
Run Code Online (Sandbox Code Playgroud)
编辑为Ignacio Vazquez-Abrams建议.(是否可以编辑而不会碰撞?)
我试图了解在Python中实例化子类时创建的对象,例如:
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
my_tesla = ElectricCar('tesla', 'model s', 2016)
Run Code Online (Sandbox Code Playgroud)
当我们创建对象my_tesla时,我们通过调用此类的构造函数来实例化类ElectricCar,该类又调用父类的构造函数.怎么会发生?现在我有两个猜测:
1)super()只是对父类的引用,因此我们通过"super().init(make,model,year)"实例化我们的子类来调用父类的构造函数.因此,我们只有一个类PowerCar()的对象.
2)super(),调用父类的构造函数,创建"Car"类的对象,然后我们通过"super().init(make,model,year)" 调用该对象的构造函数.因此,我们有两个对象:Car()类的一个对象和ElectiricCar类的一个对象,在我们的例子中它们是相同的.
哪一个是正确的?如果两者都没有,请解释一下究竟发生了什么:
super().__init__(make, model, year)
Run Code Online (Sandbox Code Playgroud) 好的,这是代码的问题
lives_group = pygame.sprite.Group()
life_pos = [603,566,529,492]
pos = 0
for life in range(0,ship.lives):
live = game_display.Life([10, life_pos[pos]])
lives_group.add(live)
Run Code Online (Sandbox Code Playgroud)
.Life ting是这样的:
class Life(pygame.sprite.Sprite):
def __init__(self, location):
self.image = pygame.image.load('Spaceship_life.png')
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\------\My programs\old\GUI based games\Spaceblaster\0.1\main.py", line 17, in <module>
lives_group.add(live)
File "C:\Python33\lib\site-packages\pygame\sprite.py", line 360, in add
sprite.add_internal(self)
File "C:\Python33\lib\site-packages\pygame\sprite.py", line 163, in add_internal
self.__g[group] = 0
AttributeError: 'Life' object has no attribute '_Sprite__g'
Run Code Online (Sandbox Code Playgroud)
对不起我的名字,但我保持它的长度以防万一
我在这里看了关于python的super()方法的其他问题,但我仍然觉得很难理解整个概念.
我也在查看pro python一书中的例子
引用的例子是
class A(object):
def test(self):
return 'A'
class B(A):
def test(self):
return 'B-->' + super(B, self).test()
class C(A):
def test(self):
return 'C'
class D(B, C):
pass
>>> A().test()
'A'
>>> B().test()
'B-->A'
>>> C().test()
'C'
>>> D().test()
'B-->C'
>>> A.__mro__
(__main__.A, object)
>>> B.__mro__
(__main__.B, __main__.A, object)
>>> C.__mro__
(__main__.C, __main__.A, object)
>>> D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, object)
Run Code Online (Sandbox Code Playgroud)
为什么要做D().test()我们得到'B - > C'而不是'B - > A'的输出
书中的解释是
在最常见的情况下,包括此处显示的用法,super()接受两个参数:类和该类的实例.正如我们在此处的示例所示,实例对象确定将使用哪个MRO来解析结果对象上的任何属性.提供的类确定该MRO的子集,因为super()仅使用在提供的类之后发生的MRO中的那些条目.
我仍然觉得解释有点难以理解.这可能是一个可能的重复,并且已经多次询问与此类似的问题,但如果我理解了这一点,我可能能够更好地理解其他问题.
我有以下代码,其中我试图扩展以下init方法BaseExporter:
from apps.ingest.platform_export import BaseExporter
class Vudu(BaseExporter):
def __init__(self):
BaseExporter.__init__()
self.platform = ' Vudu'
Run Code Online (Sandbox Code Playgroud)
基本上,我想从BaseExporter加上附加变量的所有init'd变量self.platform = 'Vudu'.我该如何正确地做到这一点?
我在Python中理解继承有一点困难.我的印象是,当Class B从Class A它继承时,继承了在A中初始化的所有值.我已经编写了一个例子来证明我的意思:
Class A():
def __init__(self,parameter):
self.initialize_parameter=4*parameter
Class B(A):
def __init__(self):
pass
def function(self,another_parameter):
return self.initialize_parameter*another_parameter
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,请致电:
B_instance=B()
print B_instance.function(10)
Run Code Online (Sandbox Code Playgroud)
返回AttributeErrorB类没有的说法self.initialized_parameter
所以我的问题是,我是否必须手动将所有初始化从A复制并粘贴到B或者是否可以在B中使用它们而不调用A类本身?(我希望这很清楚).
我收到了问题中显示的错误,我无法弄清楚原因.即使尝试其他stackoverflow方法来修复它也不起作用.
谢谢大家:)
class Item(object):
def __init__(self, name, style, quantity):
self.name = name
self.style = style
self.quantity = quantity
def itemadd(self):
inventory.append(Item)
class Weapon(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity,)
def weaponadd(self):
inventory.append(Weapon)
class Ammo(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity)
def ammoadd(self):
inventory.append(Ammo)
class Armour(Item):
def __init__(self, name, style, quantity = 1):
Item.__init__(name, style, quantity)
def armouradd(self):
inventory.append(Armour)
Bow = Weapon(name = "Bow", style = "WRanged", quantity = 1)
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢大家,这个问题已经回答:)
编辑2:受错误代码影响的行:
Traceback …Run Code Online (Sandbox Code Playgroud) class Base(object):
def __init__(self):
self.fname="MS"
self.lname="Dhoni"
class Child(Base):
def __init__(self):
self.fname="kohli"
super(Base).__init__()
Run Code Online (Sandbox Code Playgroud)
上面代码中 super 方法的用途是什么甚至评论super(Base).__init__()我正在获取输出kohli
请解释
我已经尝试过阅读一些不同的教程,但我仍然无法弄明白.我有两个简单的课程.动物和猫.
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init___(self, age):
self.age = age
print('age is: {0}'.format(self.age))
def talk(self):
print('Meowwww!')
c = Cat('Molly')
c.talk()
Run Code Online (Sandbox Code Playgroud)
输出是:
Meowwww!
Run Code Online (Sandbox Code Playgroud)
代码运行,但我有点困惑.我创建了一个cat类的实例c = Cat('Molly').因此,不知何故,通过使用"Molly"作为Cat()类实例的参数,它将提供"Molly"给原始基类(Animal)而不是Cat我创建的类实例?为什么?那么如何为Cat类实例提供age它需要的变量呢?
我试过做:
c = Cat('Molly', 10)
Run Code Online (Sandbox Code Playgroud)
但它抱怨太多的争论.其次,为什么不会__init__调用Cat类的功能?它应该打印"年龄是......".它永远不会.
编辑:得到它的工作,感谢Martijn Pieters!这是更新的代码(适用于python3):
class Animal():
def __init__(self, name):
self.name = name
print('name is: {0}'.format(self.name))
class Cat(Animal):
def __init__(self, name, age):
super().__init__(name)
self.age = age …Run Code Online (Sandbox Code Playgroud) python ×9
inheritance ×2
super ×2
class ×1
methods ×1
object ×1
polymorphism ×1
pygame ×1
python-3.3 ×1
python-3.x ×1