這是我的腳本:#錯誤:類型錯誤:不受約束的方法__init __()必須與形狀例如被稱爲第一個參數(有代替STR實例)#
class shape:
def __init__(self, name):
self.name = name
def printMyself(self):
print 'I am a shape named %s.' % self.name
shape1 = shape(name = 'myFirstShape.')
shape2 = shape(name = 'mySecondShape.')
shape1.printMyself()
shape2.printMyself()
class polyCube(shape):
def __init__(self, name, length, width, height):
shape.__init__(name)
self.length = length
self.width = width
self.height = height
def printMyself(self):
shape.printMyself(self)
print 'I am also a cube with dimensions %.2f, %.2f, %.2f.' % (length, width, height)
class polySphere(shape):
def __init__(self, name, radius):
shape.__init__(name)
self.radius = radius
def printMyself(self):
shape.printMyself(self)
print 'I am also a sphere with dimensions of %.2f.' % (radius)
cube1 = polyCube('firstCube', 2.0, 1.0, 3.0)
cube2 = polyCube('secondCube', 3.0, 3.0, 3.0)
sphere1 = polySphere('firstSphere', 2.2)
sphere2 = polySphere('secondSphere', 3.5)
shape1 = shape('myShape')
cube1.printMyself()
cube2.printMyself()
sphere1.printMyself()
sphere2.printMyself()
我的錯誤:
# Error: TypeError: unbound method __init__() must be called with shape instance as first argument (got str instance instead) #
我不明白。 爲什麼我得到這個錯誤信息? 解決方案是什麼? 爲什麼?
謝謝!
縮進會導致IndentationError,而不是TypeError。錯誤縮進幾乎肯定是複製/粘貼到SO中的結果。 – mgilson
還有其他錯誤,比如'self'不會傳遞給'__init__',他在print語句中只使用'radius'而不是'self.radius'。 –
非常感謝您 – Oldran