1. 程式人生 > >python-class(4)

python-class(4)

__init__ created time utf __add__ Coding aliyun logs usr

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: class4.py
 5 #Author: frank
 6 #Email: [email protected]
 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

python-class(4)