擅长:python、mysql、java
<p>您可以在<a href="https://docs.python.org/2/tutorial/classes.html#class-objects" rel="nofollow">the official python documentation</a>找到一个关于Python类的介绍。你知道吗</p>
<p>基本上,您希望<code>zap</code>成为一个实例方法,并且总是在类的实例上调用它。你知道吗</p>
<p>下面是您更正和扩展的示例:</p>
<pre><code>class Zapper:
def __init__(self, name):
print "My name is " + name
self.name = name
self.zap()
def zap(self):
print "Hello World, my name is still " + self.name
zing = Zapper('Frank')
zing.zap()
</code></pre>
<p>注意,我们将<code>name</code>的值存储在名为<code>self.name</code>的实例变量中。若要稍后访问此变量,需要有对实例的引用。这就是<code>zap</code>获取<code>self</code>作为第一个参数的原因。现在我们也可以在<code>zap</code>方法中使用<code>self.name</code>。你知道吗</p>
<p>在内部,点表示法转换如下:</p>
<pre><code>self.zap() => Zapper.zap(self)
zing.zap() => Zapper.zap(zing)
</code></pre>