新手求助
import mathclass Point:
def __init__(self,x = 0,y = 0):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
class Line:
def __init__(self,p1,p2):
self.x = p1.getX() - p2.getX()
self.y = p1.getY() - p2.getY()
self.len = math.sqrt((self.x**2) + (self.y**2))
def getlen(self):
return self.len
这是得到的结果,但是不知道哪里有问题。
>>> p1 = Point(1,2)
>>> p2 = Point(2,3)
>>> line = Line(p1,p2)
>>> line.getlen
<bound method Line.getlen of <__main__.Line object at 0x0000028709022340>>
你使用方法时候要带括号才算调用,否则返回的是这个函数体
import math
class Point:
def __init__(self,x = 0,y = 0):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
class Line:
def __init__(self,p1,p2):
self.x = p1.getX() - p2.getX()
self.y = p1.getY() - p2.getY()
self.len = math.sqrt((self.x**2) + (self.y**2))
def getlen(self):
return self.len
p1 = Point(1,2)
p2 = Point(2,3)
line = Line(p1,p2)
print(line.getlen())
调用函数或方法时一定要加上括号:
line.getlen()
页:
[1]