2015-08-20 37 views
0

从这里的文件,它声称超()方法(ARG)做同样的事情:超(C,个体经营)。方法(ARG)。Python 3中继承等效的语句

https://docs.python.org/3/library/functions.html#super

class shape(object): 
    def __init__(self, type): 
     self.shapeType = type 

class coloredShape1(shape): 
    def __init__(self, type, color): 
     super().__init__(type) 
     self.shapeColor = color 

class coloredShape2(shape): 
    def __init__(self, type, color): 
     super(shape, self).__init__(type) 
     self.shapeColor = color 

circle = shape("circle") 
blueRectangle = coloredShape1("rectangle", "blue") 
redSquare = coloredShape2("square", "blue") 

有与blueRectangle创作没有任何问题,但是,redSquare线引发以下厚望:

Traceback (most recent call last): 
    File "test.py", line 17, in <module> 
    redSquare = coloredShape2("square", "blue") 
File "test.py", line 12, in __init__ 
    super(shape, self).__init__(type) 
TypeError: object.__init__() takes no parameters 

我不理解这两者之间的区别。有人能解释我做错了什么吗?

回答

2

您通过错误的类到super(),所以你叫object.__init__,而不是shape.__init__

class coloredShape2(shape): 
    def __init__(self, type, color): 
     super(coloredShape2, self).__init__(type) 
     self.shapeColor = color