|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Vector2(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
@classmethod
def from_points(cls, P1, P2):
return cls( P2[0] – P1[0], P2[1] – P1[1] )
A = (10.0, 20.0)
B = (30.0, 35.0)
AB = Vector2.from_points(A, B)
print AB
- class Vector2(object):
- def __init__(self, x=0.0, y=0.0): # 调用第二步给self.x, self.y赋值
- self.x = x
- self.y = y
- def __str__(self): # 当 print 该实例时调用 ,返回
- return "(%s, %s)" % (self.x, self.y)
- @classmethod
- def from_points(cls, P1, P2):
- return cls(P2[0]-P1[0], P2[1] - P1[1]) # 第一步这个 将A B 变为(20。0,15.0)
- A = (10.0, 20.0)
- B = (30.0, 35.0)
- AB = Vector2.from_points(A, B) # 调用函数cls
- print(AB) # 这时调用AB的__str__方法 第三步
复制代码
|
|