您的位置:首页 > 编程语言 > Python开发

python类运算符的重载

2018-03-19 19:35 260 查看
alist = input().split()

blist = input().split()

n = float(input())

class Vector:
def __init__(self, x=0, y=0, z=0):
# 请在此编写你的代码(可删除pass语句)
self.X = x
self.Y = y
self.Z = z

# 代码结束

def __add__(self, other):

return Vector(self.X + other.X, self.Y + other.Y, self.Z + other.Z)

# 代码结束

def __sub__(self, other):

return Vector(self.X - other.X, self.Y - other.Y, self.Z -other.Z)

# 代码结束

def __mul__(self, other):

return Vector(self.X * other, self.Y * other, self.Z * other)
# 请在此编写你的代码(可删除pass语句)

# 代码结束

def __truediv__(self, other):

return Vector(self.X / other, self.Y / other, self.Z / other)
# 请在此编写你的代码(可删除pass语句)

# 代码结束

def __str__(self):
# 请在此编写你的代码(可删除pass语句)
return '(%.1f,%.1f,%.1f)' % (self.X, self.Y, self.Z)

a = Vector(float(alist[0]), float(alist[1]), float(alist[2]))

b = Vector(float(blist[0]), float(blist[1]), float(blist[2]))

print(a + b, a - b, a * n, a / n)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: