我已经获得了一个继承函数的类。
class shape(object):
'''Shape is an base class, which is largely intended to be abstract, that
is only used as a parent class to other classes wich define specific shapes
and inherit the shape class' fuctions getArea() and printArea()
'''
def __init__(self):
'''constructor function for the shape class
'''
self.area = None
self.name = "shape"
def getArea(self):
'''returns the area of the shape, which is defined in the constructor
'''
return self.area
def printArea(self):
'''prints a statement identifying the shape by its name and giving the
area of the shape. Returns None.
'''
print "The area of this " + self.name + " is: " + str(self.area)
return None
我必须与之合作,因此无法改变。然后,我所做的是:
class circle(shape):
"""A new class 'circle' that will inherit functions from the class 'shape'.
"""
def __init__(self, diameter):
"""A constructor function for the circle class.
"""
self.area = math.pi * (diameter ** 2 / 4.0)
self.name = circle
c = circle(10)
print c.getArea()
print c.printArea()
getArea部分工作正常,但printArea没有。我收到了标题中的错误消息。
答案 0 :(得分:2)
self.name
必须是"circle"
,而不是circle
。