理解 super() 以及有关类构造函数的问题
以下是我所引用的程序中的一段示例代码。我意识到我并没有真正的这个概念,所以我在这里问。
class Chef(games.Sprite):
"""
A chef which moves left and right, dropping pizzas.
"""
image = games.load_image("chef.bmp")
def __init__(self, y = 0, speed = 2, odds_change = 200):
""" Initialize the Chef object. """
super(Chef, self).__init__(image = Chef.image,
x = games.screen.width / 2,
y = 55,
dx = speed)
self.odds_change = odds_change
self.time_til_drop = 0
我的想法是这样的:
第六行 def __init__(self, y = 0, speed = 2, odds_change = 200) 给出了对象的初始值和内核中包含的值:
super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = 55,dx = speed)
处理对象的这些值初始化之后。使第六行值相当任意。例如,我能够将第六行构造函数中 y 的值更改为任意数字,并且该对象在屏幕上保持相同的 y 坐标。我的这个理解正确吗?
The following is a sample piece of code from a program that I'm referring to. I realized that I hadn't truly this concept, so I'm asking here.
class Chef(games.Sprite):
"""
A chef which moves left and right, dropping pizzas.
"""
image = games.load_image("chef.bmp")
def __init__(self, y = 0, speed = 2, odds_change = 200):
""" Initialize the Chef object. """
super(Chef, self).__init__(image = Chef.image,
x = games.screen.width / 2,
y = 55,
dx = speed)
self.odds_change = odds_change
self.time_til_drop = 0
This is what I thought is the case:
the sixth line def __init__(self, y = 0, speed = 2, odds_change = 200)
gives the object its initial values and the values enclosed in the kernel:
super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = 55,dx = speed)
deal with these values of the object after initialization. Making the sixth line values fairly arbitary. For example I was able to change the value of y in the sixth line constructor to any arbitary number, and the object stayed in the same y coordinate on the screen. Is my understanding of this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。
= 0
、= 2
等不会初始化对象 - 它们只是函数的默认参数。例如:在您编写的代码中,这意味着您只需调用
Chef()
,并在构造函数中调用变量y
、speed
的值code> 和odds_change
将是默认值。但这并没有设置实例变量。设置它们的代码位于
games.Sprite
的构造函数(Chef
的超类)中。Naw. The
= 0
,= 2
, etc., don't initialize the object - they're just default arguments to a function. Eg:In the code you wrote, that means you can just call
Chef()
, and in the constructor, the values for the variablesy
,speed
, andodds_change
will be the default values.This doesn't set the instance variables, though. The code that sets them is in
games.Sprite
's constructor (the superclass ofChef
).super实际上所做的就是调用父类相应的方法。 http://docs.python.org/library/functions.html#super
因此,在您的情况下,您正在从定义的类
Chef
调用类games.Sprite
的__init__
方法您获得该对象的原因尽管您在第六行中更改了 y,但在同一位置,因为您没有更改传递给
super
函数的参数y=55
。如果你将这个 y 更改为其他值,你的物体肯定会移动。使用 super 时的一般做法是传递与从 __init__ 方法获取的参数相同的参数。在您的代码中,您没有将
__init__
的任何参数传递给super(Chef, self).__init__
,因此未将__init__
中定义的参数传递给super(Chef, self).__init__
> 或多或少没有意义。所以你的代码应该是这样的 -Rem,你可以在 __init__ 方法中获取更多参数,然后传递给 super(Chef, self).__init__ 函数。
What
super
actually does is calls the parent class' corresponding method. http://docs.python.org/library/functions.html#superSo, in your case, you are calling the
__init__
method of the classgames.Sprite
from your defined classChef
The reason you got the object at the same position although you changed y in the sixth line because you didn't change the parameter
y=55
which you are passing to thesuper
function. If you will change this y to something else, your object will definitely move.The general practice while using
super
is to pass the same parameters as you get from the__init__
method. In your code, you are not passing any of the parameters of__init__
tosuper(Chef, self).__init__
and hence the parameters defined in__init__
are more or less meaningless. So your code should be some what like this -Rem, you can take more parameters in the
__init__
method then you pass tosuper(Chef, self).__init__
function.