擅长:python、mysql、java
<p>在第一个示例中,传递了<em>类</em>作为参数,而不是您创建的<em>实例</em>。因为类<code>Rectangle</code>中没有<code>self.x</code>,因此引发了错误。在</p>
<p>只需将函数放入类中:</p>
<pre><code>class Rectangle:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def moveRect(self, dx, dy):
self.x += dx
self.y += dy
rect = Rectangle(3.0, 4.0, 50, 120)
dx = raw_input("enter dx value:")
dy = raw_input("enter dy value:")
rect.moveRect(float(dx), float(dy))
</code></pre>