擅长:python、mysql、java
<p>为了补充上述出色的答案,只需在父构造函数中添加以下行,就可以更好地了解发生了什么:</p>
<pre><code>class Resistor(object):
def __init__(self, ohms):
print (type(self).__name__)
self.ohms = ohms
</code></pre>
<p>它将打印<code>VoltageResistor</code>,然后抛出一个<code>ValueError</code>。<a href="https://docs.python.org/3/library/functions.html" rel="nofollow">Python docs</a>确认这一点:</p>
<blockquote>
<p>If <strong>c is an instance of C</strong>, c.x will invoke the getter, <strong>c.x = value will invoke the setter</strong> and del c.x the deleter.</p>
</blockquote>