【发布时间】:2015-09-18 00:24:35
【问题描述】:
我用几种方法创建了一个父类(几何)和子类(点)。我正在尝试使用这两个类中的方法在测试函数中运行进程。我很难在函数中访问这些方法。我在测试函数中创建了三个点,P1 = (0,3)、P2 = (-3,7) 和 P3 = (-3,7),并想对它们执行任务。例如, def init(self, x, y): 假设返回测试函数中列出的点的 x,y 坐标。我尝试了以下所有方法:print(test(Point(p1)))、print test(p1.Point) 和 print p1.Point。他们都没有工作。有人可以提供有关我如何执行该操作的指导吗?
我的代码:
class Geometry(object):
uniqueID = -1
def __init__(self):
Geometry.uniqueID += 1
self.id = Geometry.uniqueID
class Point(Geometry):
def __init__(self, x, y):
Geometry.__init__(self)
self.x = x
self.y = y
def __str__(self):
return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)
def __eq__(self, point):
if type(point) is Point:
if self.x == point.x and self.y == point.y:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def identify(self, other):
if type(other) is Point:
if self.id == other.id:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def distance(self, other):
if type(other) is Point:
x_square = (other.x - self.x) ** 2
y_square = (other.y - self.y) ** 2
d = (x_square + y_square) ** 0.5
return float(d)
else:
raise Exception("The input parameter has to be a Point object!")
def quadrant(self):
output = ""
if self.x > 0 and self.y > 0:
output = "Quad I"
elif self.x < 0 and self.y > 0:
output = "Quad II"
elif self.x < 0 and self.y < 0:
output = "Quad III"
elif self.x > 0 and self.y < 0:
output = "Quad IV"
elif self.x == 0:
output = "Y-axis"
elif self.y == 0:
output = "X-axis"
elif self.x == 0 and self.y == 0:
output = "Origin"
return output
def test(p1,p2,p3):
p1 = Point(0, 3)
p2 = Point(-3, 7)
p3 = Point(-3, 7)
def collinear(p1, p2, p3):
p1x = p1.x
p1y = p1.y
# m1 = ?
# m2 = ?
def main():
print (p1.Point)
if __name__ == "__main__":
main()
【问题讨论】:
标签: python python-2.7 oop