我有具有2个属性的类坐标:Long
,Lat
。如何使用另一个类的对象创建类
那么我该如何创建具有类Coordinat
的一个或多个对象的类多边形?
class Coordinat:
def __init__(self, id, Lat, Long):
self.id=id
self.Lat=Lat
self.Long=Long
那么我该如何创建类多边形?
我有具有2个属性的类坐标:Long
,Lat
。如何使用另一个类的对象创建类
那么我该如何创建具有类Coordinat
的一个或多个对象的类多边形?
class Coordinat:
def __init__(self, id, Lat, Long):
self.id=id
self.Lat=Lat
self.Long=Long
那么我该如何创建类多边形?
how to create a class using object from another class
嗯,这是已经你在你的“Coordinat”级做什么:它使用三个“对象从另一个阶级”的id
,Lat
和Long
(我假设要么int或字符串第一个和第二个和第三个花车)。请记住,在Python只是一切都是对象...
在你的情况,因为一般的多边形没有角度固定数量的,你想要的Coordinat
有序序列(tuple
或list
):
class Polygon(object):
def __init__(self, **coords):
self.coords = coords
triangle = Polygon(
Coordinat(1, 42.5, 4.5),
Coordinat(2, 85.4, 77.2),
Coordinat(3, 99.1, 42.2)
)
我使用你的方式,但如何访问拉特和长从一个协调...例如,我想计算三角形协调之间的距离... –
'triangle.coords [0] .Lat'有什么问题?请注意,对于正常的多边形,你可能想要具体的类... –
我再次使用你的方式。但是当我为多边形的对象设置值时会出现另一个错误。 我的代码:'point = [] point.append(Coordinat(1,22,33)) point.append(Coordinat(2,45,66)) 多边形(ID,距离,点)' 错误:多边形(ID,距离,点)) TypeError:__init __()只需要3个参数(4给出) –
正如在评论中提到你应该使用继承,你可以看看文档here。
在你的情况,你可以做这样的事情:
class Coordinat:
def __init__(self, id, Lat, Long):
self.id=id
self.Lat=Lat
self.Long=Long
class Polygon(Coordinat):
def __init__(self, id, Lat, Long, prop1, prop2)
#Call __init__ method from base class to have id, Lat and Long initialized
Coordinat.__init__(id, Lat, Long)
#Initiailze the other properties
self.prop1 = prop1
self.prop2 = prop2
...
多边形如何是一种“协调”类型? –
哪个Python版本您使用的?请标记python 2或python 3. – toti08
删除如何从你的问题,你已经在Python文档中的答案 –
@AvinashRaj你会不会解释一个多边形究竟是一个“种”坐标? OP要求的关系是“有一个”(在这种情况下“有一组”),而不是“是”。 –