# _*_ coding: utf-8 _*_
__author__ = 'pythonwu'
__date__ = "2018/5/11 18:21"

from math import hypot

class Vecotr:
def __init__(self,x=0,y=0):
self.x = x
self.y = y

def __repr__(self):
return "Vector(%r,%r)" %(self.x,self.y)

def __abs__(self):
return hypot(self.x,self.y)

def __bool__(self):
return bool(self.x or self.y)

def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vecotr(x,y)

def __mul__(self, scalar):
return Vecotr(self.x*scalar,self.y*scalar)

注释:其实最简单是使用complex类来实现 ,但是当需要多维向量的时候就不是很方便了,这个类可以扩展至n维向量
注释:自定义对象bool方法(内置)__bool__方法不存在的时候,调用__len__判断真假

相关文章:

  • 2022-12-23
  • 2021-12-03
  • 2021-04-29
  • 2021-06-21
  • 2021-11-02
  • 2021-06-01
  • 2021-07-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-11-19
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案