0

这是我的脚本:

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) # 

我不明白。为什么我收到此错误消息?解决方案是什么?为什么?

谢谢!

4

2 回答 2

2

您的代码的工作版本,我已经在评论中解释了错误

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__(self,name) #pass self here, you're calling parent's __init__() explicitly so you should pass self.

        self.length = length
        self.width = width
        self.height = height

    def printMyself(self):
     shape.printMyself(self)
     #use self.length ,self.width instead of just length,width etc
     print ('I am also a cube with dimensions %.2f, %.2f, %.2f.' % (self.length, self.width, self.height)) 


class polySphere(shape):
    def __init__(self, name, radius):
        shape.__init__(self,name) #pass self here

        self.radius = radius

    def printMyself(self):
     shape.printMyself(self)
     print ('I am also a sphere with dimensions of %.2f.' % (self.radius)) #use self.radius here

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()
于 2012-06-26T14:20:19.207 回答
0

通常,您应该发布完整的回溯。它使事情更容易调试。问题(除了我认为来自复制/粘贴错误的缩进)是当您调用时:

shape.__init__(name)

当您从形状继承时。

如果您查看shape.__init__' 的“原型”,它看起来像shape.__init__(self,name)- 这就是您应该使用的。错误来了,因为你正在传递name(一个字符串)你应该传递的地方self(a PolyCube,因此也是shape由于继承)

在旁边

此外,在 python 2.x 中,始终从object. 例如:

class shape(object):
   ...

这允许您使用与新型类相关的所有优点。(在python 3中,所有类都是新式类)

于 2012-06-26T14:25:31.007 回答