|
楼主 |
发表于 2017-8-12 17:36:15
|
显示全部楼层
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals,division,print_function
- class Num():
- def __init__(self,value=0):
- self.value=value
- def __add__(self,value):
- if isinstance(value,int):
- return Num(self.value+value)
- elif isinstance(value,Num):
- return Num(self.value+value.value)
- def __radd__(self, other):
- # print(other)
- self=self.__add__(other)
- return self
- def __str__(self):
- return str(self.value)
- __repr__=__str__
-
- X=Num(10)
- Y=X+15
- Z=1000+X
- print('Y==%s,Z==%s'%(Y.value,Z.value))
- print(type(Z))
- Z+=1
- print('Z==%s'%Z.value)
- #输出
- #Y==25,Z==1010
- #<type 'instance'>
- #Z==1011
复制代码
经过测试确实有效,感谢~! |
|