Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用类';我们换班回来吧?_Python - Fatal编程技术网

Python 如何使用类';我们换班回来吧?

Python 如何使用类';我们换班回来吧?,python,Python,这是我写的。一个关于几何对象的程序,我正在尝试在类Circle的str函数中使用GeometricObject.str(self),但它不起作用。我尝试了很多方法,但仍然有一些错误 import math class GeometricObject(object): def __init__(self, color = "white", filled = True): self.color = color self.filled = filled

这是我写的。一个关于几何对象的程序,我正在尝试在类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)
>>> 
输入圆的输入值
输入半径值:1
回溯(最近一次呼叫最后一次):
文件“C:\Users\wxwdd\u 000\Desktop\CSC131\Lab\u 5.py”,第94行,在
main()
文件“C:\Users\wxwdd\u 000\Desktop\CSC131\Lab\u 5.py”,第71行,主目录
打印c1
文件“C:\Users\wxwdd\u 000\Desktop\CSC131\Lab\u 5.py”,第32行,在__
几何对象。\uuuu str\uuuuuu(自)
TypeError:必须使用GeometricObject实例作为第一个参数调用未绑定的方法_ustr__;()
>>> 

如何修复str函数?

您忘了让子类继承自
GeometricObject

改变这些:

class Circle(object):
class Triangle(object):
对这些:

class Circle(GeometricObject):
class Triangle(GeometricObject):

附加说明:您可能应该调用
GeometricObject.\uuuu init\uuuu
来设置属性
Triangle
Circle
继承自
GeometricObject
,而不是直接分配它们。如果
GeometricObject.\uuuuu init\uuuuuu
对其参数做了更复杂的操作,子类构造函数要么不能正确处理它,要么与超类的实现细节联系太紧密。

如果要继承它,应该是这样的
类circle(GeometricObject):
也可以使用内置函数调用调用GeometricObjects str方法,比如
super(circle,self).str(arg)


希望这有帮助

您的意思是圆和三角形继承自GeometricObject吗?