|
3鱼币
- class Nstr:
- def __init__(self, arg=''):
- if isinstance(arg, str):
- self.total = 0
- for each in arg:
- self.total += ord(each)
- else:
- print("参数错误!")
- def __add__(self, other):
- return self.total + other.total
- def __sub__(self, other):
- return self.total - other.total
- def __mul__(self, other):
- return self.total * other.total
- def __truediv__(self, other):
- return self.total / other.total
- def __floordiv__(self, other):
- return self.total // other.total
复制代码
def __add__(self, other):
return self.total + other.total 这句返回值是什么意思 求教一下self.total + other.total
self是第一个参数。在python里是指“实例”本身。就是自己。 这个class Nstr有一个属性是total
other是第二个参数,它代表另一个class Nstr的实例。当然它也有一个属性total
__add__是一个重载加号的函数。意思是将两个class Nstr实例相加,结果等于两个实例的x变量相加之和。
|
最佳答案
查看完整内容
self是第一个参数。在python里是指“实例”本身。就是自己。 这个class Nstr有一个属性是total
other是第二个参数,它代表另一个class Nstr的实例。当然它也有一个属性total
__add__是一个重载加号的函数。意思是将两个class Nstr实例相加,结果等于两个实例的x变量相加之和。
|