python 面向对象调用问题
#-*-coding:cp936-*-#python27#xiaodeng#[面向对象]运算符重载classMylist():__mylist=[]def__init__...
# -*- coding: cp936 -*-
#python 27
#xiaodeng
#[面向对象]运算符重载
class Mylist():
__mylist=[]
def __init__(self,*args):#重载__init__方法
self.__mylist=[]
for arg in args:
self.__mylist.append(arg)
print self.__mylist
def __add__(self,n):
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]+n
return self.__mylist
def __sub__(self,n):#重载减法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]-n
def __mul__(self,n):#重载乘法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]*n
def __div__(self,n):#重载除法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]/n
def __len__(self):
return len(self.__mylist)
def show(self):
print self.__mylist
请问大神们,这个class该怎么调用__sub__等办法,能麻烦帮忙举例子吗? 展开
#python 27
#xiaodeng
#[面向对象]运算符重载
class Mylist():
__mylist=[]
def __init__(self,*args):#重载__init__方法
self.__mylist=[]
for arg in args:
self.__mylist.append(arg)
print self.__mylist
def __add__(self,n):
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]+n
return self.__mylist
def __sub__(self,n):#重载减法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]-n
def __mul__(self,n):#重载乘法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]*n
def __div__(self,n):#重载除法运算
for i in range(0,len(self.__mylist)):
self.__mylist[i]=self.__mylist[i]/n
def __len__(self):
return len(self.__mylist)
def show(self):
print self.__mylist
请问大神们,这个class该怎么调用__sub__等办法,能麻烦帮忙举例子吗? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询