python-class(4)

 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
原文地址:https://www.cnblogs.com/black-mamba/p/7476806.html