[英]Creating a List and Summing Attribute in that list of Python Objects
我有一類具有屬性長度的對象。 點類有一個(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)
如果我理解正確,那么您需要的是:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.