0

我正在尝试这个

In [1]: class Parent:
   ...:     def __init__(self):
   ...:         self.a =10
   ...:         self.b =20
   ...:


In [3]: class NewParent(Parent):
    def __init__self():
        super(NewParent,self).__init__()
        self.c =30
   ...:

当我这样做时

In [4]: c = NewParent()

In [5]: c
Out[5]: <__main__.NewParent instance at 0x2c98878>

In [6]: c.a
Out[6]: 10

In [7]: c.b
Out[7]: 20

In [8]: c.c

() ----> 1 cc 中的 AttributeError Traceback (最近一次调用最后一次)

AttributeError:NewParent 实例没有属性“c”

4

2 回答 2

1

你搞砸了NewParent.

def __init__(self):
于 2013-02-09T09:11:50.247 回答
0
class NewParent(Parent):
    def __init__self():
        super(NewParent,self).__init__()
        self.c =30

应该

class NewParent(Parent):
    def __init__(self):
        #Your code here
于 2013-02-09T09:14:26.593 回答