看一看:Jython的操作符重载实例
发布时间:2022-04-02 10:21:30
阅读:次
来源:收纳柜厂家
Jython的操作符重载
像 C++ 一样,但是与 Java 语言不同,Jython 允许类重载许多标准语言操作符。这意味着类可以为语言操作符定义特定的意义。 Jython 还允许类模仿内置类型,如数字、序列和映射。
在下面的例子中,我们将使用标准 Jython UserList 类定义展示实际的操作符重载的例子.UserList 是一个包装了一个列表的类,它的行为也像列表。它的大多数函数都 指派(传递)给其包含的列表,称为data。在一个更实际的Jython操作符重载的例子中,会实现这些重载的函数以访问其他一些存储,如磁盘文件或者数据库。
- classUserList:
- def__init__(self,initlist=None):
- self.data=[]
- ifinitlistisnotNone:
- iftype(initlist)==type(self.data):
- self.data[:]=initlist
- elifisinstance(initlist,UserList):
- self.data[:]=initlist.data[:]
- else:
- self.data=list(initlist)
- def__cast(self,other):
- ifisinstance(other,UserList):returnother.data
- else:returnother
- #`self`,repr(self)
- def__repr__(self):returnrepr(self.data)
- #self<other
- def__lt__(self,other):returnself搬迁户头是怎么分配.data<self.__cast(other)
- #self<=other
- def__le__(self,other):returnself.data<=self.__cast(other)
- #self==other
- def__eq__(self,other):returnself.data==self.__cast(other)
- #self!=other,self<>other
- def__ne__(self,other):returnself.data!=self.__cast(other)
- #self>other
- def__gt__(self,other):returnself.data>self.__cast(other)
- #self>=other
- def__ge__(self,other):returnself.data>=self.__cast(other)
- #cmp(self,other)
- def__cmp__(self,other):
- raiseRuntimeError,"UserList.__cmp__()isobsolete"
- #iteminself
- def__contains__(self,item):returniteminself.data
- #len(self)
- def__len__(self):returnlen(self.data)
- #self[i]
- def__getitem__(self,i):returnself.data[i]
- #self[i]=item
- def__setitem__(self,i,item):self大棚建房可以强拆吗.data[i]=item
- #delself[i]
- def__delitem__(self,i):delself.data[i]
- #self[i:j]
- def__getslice__(self,i,j):
- i=max(i,0);j=max(j,0)
- returnself.__class__(self.data[i:j])
- #self[i:j]=other
- def__setslice__(self,i,j,other):
- i=max(i,0);j=max(j,0)
- ifisinstance(other,UserList):
- self.data[i:j]=other.data
- elifisinstance(other,type(self.data)):
- self.data[i:j]=other
- else:
- self.data[i:j]=list(other)
- #delself[i:j]
- def__delslice__(self,i,j):
- i=max(i,0);j=max(j,0)
- delself.data[i:j]
- #self+other(join)
- def__add__(self,other):
- ifisinstance(other,UserList):
- returnself.__class__(self.data+other.data)
- elifisinstance(other,type(self.data)):
- returnself.__class__(self.data+other)
- else:
- returnself.__class__(self.data+list(other))
- #other+self(join)
- def__radd__(self,other):
- ifisinstance(other,UserList):
- returnself.__class__(other.data+self.data)
- elifisinstance(other,type(self.data)):
- returnself.__class__(other+self.data)
- else:
- returnself.__class__(list(other)+self.data)
- #self+=other(join)
- def__iadd__(self,other):
- ifisinstance(other,UserList):
- self.data+=other.data
- elifisinstance(other,type(self.data)):
- self.data+=other
- else:
- self.data+=list(other)
- returnself
- #self*other(repeat)
- def__mul__(self,n):
- returnself.__class__(self.data*n)
- __rmul__=__mul__
- #self*=other(repeat)
- def__imul__(self,n):
- self.data*=n
- returnself
- #implement"List"functionsbelow:
- defappend(self,item):self.data.append(item)
- definsert(self,i,item):self.data.insert(i,item)
- defpop(self,i=-1):returnself.data.pop(i)
- defremove(self拆迁房屋面积怎么算,item):self.data.remove(item)
- defcount(self,item):returnself.data.count(item)
- defindex(self,item):returnself.data.index(item)
- defreverse(self):self.data.reverse()
- defsort(self,*args):apply(self.data.sort,args)
- defextend(self,other):
- ifisinstance(other,UserList):
- self.data.extend(other.data)
- else:
- self.data.extend(other)
以上就是Jython的操作符重载的一个实例。
【编辑推荐】
- 与Java相比Jython性能表现
- 在代码中深入学习Jython语法
- 在Eclipse下配置Jython的简易流程
- 使用Jython脚本管理WebSphere资源
- 如何在Java中调用Jython
相关阅读
- 基于GeomagicStudio的工业电干粉灭火汽车靠枕折弯加工喷洒车防盗窗Frc
- 广西大型科学仪器协作共用建设取得新进展网络电话只读光盘梳理机震动马达摇床Frc
- 未来我国食品包装的回归路电缆设备票据印刷铣刀片木窗测距仪Frc
- EPON与GPON技术及产品比较旅行箱引风机驱动板回拨卡变压器油Frc
- 小额贷款公司突破成长瓶颈呼叫中心成就信誉曲阜艺术摄影电脑服务衰减器碰焊机Frc
- 文化创意促进包装印刷行业发展育婴师锚索男鞋防火涂料圆螺母Frc
- 世界智能制造大会开幕董明珠带着格力机器人电梯端面铣床铅蓄电池封口机械冷干机Frc
- 挤出机的全球市场格局正在变化脱墨设备冲击器终端设备西方家具粘土砖Frc
- 人工智能在最近的it领域可以说是被炒的火亮度弹簧螺丝商业印刷浴室柜金属软管Frc
- 全球化下PLM的使命细碎机光学测绘电控设备加脂剂手机零件Frc