python 类的重载

#!/usr/bin/python
#
_*_coding:utf8_*_
class money():
def __init__(self,currency,value,accuracy):
self.currency=currency
self.value=value
self.accuracy=accuracy
def __add__(self,other):
return money(self.currency,self.value+other.value,self.accuracy)
def __sub__(self,other):
return money(self.currency,self.value-other.value,self.accuracy)
def __rmul__(self,other):
return money(self.currency,self.value*other.value,self.accuracy)
# def __rmul__(self,other):
#
return self.value*other
rmbAccuracy=["","",""]
rmb1=money("rmb",20,rmbAccuracy)
rmb2=money("rmb",50,rmbAccuracy)
accRmb = rmb1 + rmb2
subRmb = rmb1 - rmb2
print "重载和为:"+str(accRmb.value)+"重载之差:"+str(subRmb.value)
mulRmb = rmb1 * rmb2 * rmb1
print "类的重载乘积为:"+str(mulRmb.value)
#mulIntRmb = rmb1 *28
#
print "类与整数的乘积为" +str(mulIntRmb)

转载于:https://www.cnblogs.com/wuxi/archive/2011/12/15/2289443.html

你可能感兴趣的:(python 类的重载)