1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: class4.py
 5 #Author: frank
 6 #Email: frank0903@aliyun.com
 7 #Created Time:2017-09-04 16:36:31
 8 ############################
 9 
10 #运算符重载
11 class Vector:
12     def __init__(self, a, b):
13         self.a = a
14         self.b = b
15 
16     def __str__(self):
17         return 'Vector (%d, %d)' % (self.a, self.b)
18     
19     def __add__(self,other):
20         return Vector(self.a + other.a, self.b + other.b)
21 
22 v1 = Vector(2,10)
23 print v1
24 v2 = Vector(5,-2)
25 print v1 + v2

 

相关文章:

  • 2021-12-09
  • 2021-07-28
  • 2021-06-24
  • 2021-10-22
  • 2021-11-15
  • 2021-09-25
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2021-10-01
  • 2021-12-31
  • 2021-08-28
  • 2021-09-16
  • 2021-05-30
  • 2022-02-12
  • 2021-05-29
相关资源
相似解决方案