这是我写的。关于几何对象的程序,我试图在Circle的str函数中使用GeometricObject。 str (self),但它不起作用。我尝试了很多方法,但仍然有一些错误。
import math
class GeometricObject(object):
def __init__(self, color = "white", filled = True):
self.color = color
self.filled = filled
def getColor(self):
return self.color
def setColor(self, color):
self.color = color
def isFilled(self):
return self.filled
def setFilled(self, filled):
self.filled = filled
def __str__(self):
return "color: " + self.color + \
" and filled: " + str(self.filled)
class Circle(object):
def __init__(self, radius = 1, color = "white", filled = True):
self.radius = radius
self.color = color
self.filled = filled
def __str__(self):
return "Circle: radius = " + str(self.radius) + \
GeometricObject.__str__(self)
def getArea(self):
return (math.pi * self.radius**2)
def getPerimeter(self):
return (math.pi * 2 * self.radius)
class Triangle(object):
def __init__(self, side1 = 1, side2 = 1, side3 = 1, color = "white", filled = True):
self.side1 = side1
self.side2 = side2
self.side3 = side3
self.color = color
self,filled = filled
def __str__(self):
return "Triangle: side1 = " + str(self.side1) + \
" side2 = " + str(self.side2) + \
" side3 = " + str(self.side3) + \
" color: " + str(self.color) +\
" filled: " + str(self.filled)
def getArea(self):
s = (side1 + side2 + side3) / 2
area = sqrt(s(s - side1)(s - side2)(s - side3))
return area
def getPerimter(self):
return (side1 + side2 + side3)
def main():
#Testing Circle class
print "Entering input values for a circle"
r = input('Enter value for radius: ')
c1 = Circle(r)
print c1
print "%.2f" % c1.getArea()
print "%.2f" % c1.getPerimeter()
print c1.getColor()
print c1.isFilled()
#Testing Triangle class
print "\nEntering input values for a traingle"
s1 = input('Enter value for side1: ')
s2 = input('Enter value for side2: ')
s3 = input('Enter value for side3: ')
color = raw_input('Enter color of the triangle: ')
filled = input('Is the triangle filled (1/0)? ')
filled = (filled == 1)
t1 = Triangle(s1, s2, s3, color, filled)
print t1
print "%.2f" % t1.getArea()
print "%.2f" % t1.getPerimeter()
print t1.getColor()
print t1.isFilled()
main()
但它一直告诉我,
Entering input values for a circle
Enter value for radius: 1
Traceback (most recent call last):
File "C:\Users\wxwdd_000\Desktop\CSC131\Lab_5.py", line 94, in <module>
main()
File "C:\Users\wxwdd_000\Desktop\CSC131\Lab_5.py", line 71, in main
print c1
File "C:\Users\wxwdd_000\Desktop\CSC131\Lab_5.py", line 32, in __str__
GeometricObject.__str__(self)
TypeError: unbound method __str__() must be called with GeometricObject instance as first argument (got Circle instance instead)
>>>
如何修复 str 功能?
答案 0 :(得分:0)
您忘记了子类继承自GeometricObject
。
更改这些:
class Circle(object):
class Triangle(object):
到这些:
class Circle(GeometricObject):
class Triangle(GeometricObject):
附加说明:您应该致电GeometricObject.__init__
来设置属性Triangle
和Circle
继承自GeometricObject
,而不是直接分配它们。如果GeometricObject.__init__
使用其参数做了更复杂的事情,子类构造函数将无法正确处理它或者与超类的实现细节过于依赖。
答案 1 :(得分:0)
我想继承它应该是这样的
class circle(GeometricObject):
也可以使用super()内置函数调用调用GeometricObjects str方法,如super(circle,self).str(arg)
希望这有帮助