python的专用类方法

#!/usr/bin/python
import math
class Vector:
   def __init__(self, a, b):
      self.a = a
      self.b = b

   def __str__(self):
      return 'Vector (%d, %d)' % (self.a, self.b)
   
   def __add__(self,other):
      return Vector(self.a + other.a, self.b + other.b)

   def __abs__(self):

          return  math.sqrt(pow(self.a,2)+pow(self.b,2))


v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2

print abs(v1)

 
原文地址:https://www.cnblogs.com/buptmemory/p/2842774.html