我正在尝试制作这样的Python模块:
class square:
def _init_(self):
self._length = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = float(self._length * 4)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._length)
return self._area
class rectangle:
def _init_(self):
self._length = 0
self._width = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def setWidth(self, width):
self._width = float(width)
self._perimeter = 0
self._area = 0
def getWidth(self):
return self._width
def getPerimeter(self):
if self._perimeter == 0:
perim1 = float(self._length * 2)
perim2 = float(self._width * 2)
self._perimeter = float(perim1 + perim2)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._width)
return self._area
class circle:
def _init_(self):
self._radius = 0
self._diameter = 0
self._circumference = 0
self._pi = 3.14159265
def setRadius(self, radius):
self._radius = float(radius)
self._diameter = float(self._radius * 2)
self._circumference = 0
def setDiameter(self, diameter):
self._diameter = float(diameter)
self._radius = float(self._diameter / 2)
self._circumference = 0
def getRadius(self):
return self._radius
def getDiameter(self):
return self._diameter
def getPi(self):
return self._pi
def getCircumference(self):
if self._circumference == 0:
self._circumference = float(self._diameter * self._pi)
return self._circumference
class triangle:
def _init_(self):
self._side = []
self._side[0] = 0
self._side[1] = 0
self._side[2] = 0
self._side[3] = 0
self._angle = []
self._angle[0] = 0
self._angle[1] = 0
self._angle[2] = 0
self._angle[3] = 0
self._perimeter = 0
self._area = 0
def setSide(self, side, length):
self._side[side] = float(length)
def getSide(self, side):
return self._side[side]
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = side[1] + side[2] + side[3]
return self._perimeter
def setAngle(self, angle, measure):
self._angle[angle] = float(measure)
def getAngle(self, angle):
if self._angle[angle] == 0:
if angle == 1:
angle1 = self._angle[2]
angle2 = self._angle[3]
elif angle == 2:
angle1 = self._angle[1]
angle2 = self._angle[3]
elif angle == 3:
angle1 = self._angle[1]
angle2 = self._angle[2]
anglet = angle1 + angle2
angler = float(180) - anglet
self._angle[angle] = angler
return self.angle[angle]
它是名为Mathworks的包的一部分。调用代码是这样的:
import mathworks as mw
mycircle = mw.shapes.circle()
mycircle.setDiameter(5)
circum = mycircle.getCircumference()
print circim
当我尝试两个运行第二个模块时,我得到了这个:
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "<module1>", line 21, in <module>
File "<module1>", line 17, in main
File "C:\Python27\lib\site-packages\mathworks\shapes.py", line 70, in getCircumference
self._circumference = float(self._diameter * self._pi)
AttributeError: circle instance has no attribute '_pi'
怎么了?如果我用3.14159265替换self._pi
,它可以工作,但我需要它以另一种方式工作。
答案 0 :(得分:5)
您没有正确命名初始值设定项,它在任何一端都需要 double 下划线:
def __init__(self):
您需要更正所有您的课程。
因为您没有正确命名它们,所以在创建类的实例时它们不会被运行。
您遇到的下一个问题是triangle
初始化程序;您无法处理空列表中的索引。改为一次创建整个列表:
def __init__(self):
self._side = [0, 0, 0, 0]
self._angle = [0, 0, 0, 0]
self._perimeter = 0
self._area = 0
答案 1 :(得分:1)
您的构造函数写错了。它应该是__init__
,而不是_init_
,即双倍下划线。
因为你写错了,所以没有被调用,因此在实例化对象时不会创建这些属性。