在该Python对象列表中创建列表和汇总属性

时间:2018-11-23 00:13:09

标签: python python-2.7

我有一类具有属性长度的对象。点类有一个(x,y)点,线类有这两个点并计算2个笛卡尔(x,y)点之间的距离并给出了线的长度。我想创建一个包含空列表的多边形类,并从Line类获取Line值并将其添加到列表中。任何建议,将不胜感激!

class Point(object):
    def __init__(self,x,y):
        self.__x=float(x)
        self.__y=float(y)

    @property
    def x(self):
        return self.__x

    @property
    def y(self):
        return self.__y

    @property
    def calculation(self):
        return (self.__x,self.__y)

    def __iter__(self):
        for i in (self.calculation):
            yield i

class Line(object):
    def __init__(self,fromPoint,toPoint):
        self.__fromPoint= tuple(fromPoint)
        self.__toPoint= tuple(toPoint)

    @property
    def length(self):
        return self.__length

    def calc(self):
        self.__length = ((self.__fromPoint[0] - self.__toPoint[0]) + (self.__fromPoint[1] - self.__toPoint[1]))**0.5

class Polygon(object):
    def __init__(self):
        self.__segments= []
        self.__length= float(length)

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么类似的就是您所需要的:

class Polygon(object):
    def __init__(self, *points):
        self.points = points
        self.lines = []

    def create():
        points = self.points
        i = 0
        while i < len(points):
            if i + 1 == len(points):
                line = Line(points[i], points[0])
                self.lines.append(line)
                break
            line = Line(points[i], points[i + 1])
            self.lines.append(line)
            i += 1