这是我的脚本:
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) #
我不明白。为什么我收到此错误消息?解决方案是什么?为什么?
谢谢!