- 论坛徽章:
- 0
|
回复 1# ideal_gjh
这个例子是哪里来的啊,给你看个例子:http://www.tutorialspoint.com/python/python_classes_objects.htm- Overloading Operators:
- Suppose you've created a Vector class to represent two-dimensional vectors. What happens when you use the plus operator to add them? Most likely Python will yell at you.
- You could, however, define the __add__ method in your class to perform vector addition, and then the plus operator would behave as per expectation:
- EXAMPLE:
- #!/usr/bin/python
- class Vector:
- def __init__(self, a, b):
- self.a = a
- self.b = b
- def __str__(self):
- return 'Vector (%d, %d)' % (self.a, self.b)
-
- def __add__(self,other):
- return Vector(self.a + other.a, self.b + other.b)
- v1 = Vector(2,10)
- v2 = Vector(5,-2)
- print v1 + v2
- When the above code is executed, it produces following result:
- Vector(7,8)
复制代码 |
|