1

我正在尝试制作这样的 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'

怎么了?如果我self._pi用 3.14159265 替换它可以工作,但我需要它以另一种方式工作。

4

2 回答 2

5

您没有正确命名初始化程序,它的两端都需要双下划线:

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
于 2013-03-11T20:56:04.467 回答
1

您的构造函数编写不正确。它应该是__init__,不是_init_,即双下划线。

因为你写错了,它没有被调用,所以当你实例化你的对象时没有创建这些属性。

于 2013-03-11T20:56:33.920 回答