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エラーが発生するのは、渡す必要のある場所(文字列)を渡すためですselfPolyCubeしたがって、shape継承によるものです) 。

傍白

また、python 2.xでは、常にから継承することをお勧めしobjectます。例えば:

class shape(object):
   ...

これにより、新しいスタイルのクラスに関連するすべての長所を使用できます。(Python 3では、すべてのクラスが新しいスタイルのクラスです)

于 2012-06-26T14:25:31.007 に答える