收纳柜厂家
免费服务热线

Free service

hotline

010-00000000
收纳柜厂家
热门搜索:
成功案例
当前位置:首页 > 成功案例

看一看:Jython的操作符重载实例

发布时间:2022-04-02 10:21:30 阅读: 来源:收纳柜厂家
Jython的操作符重载实例 作者:佚名 2009-07-14 18:34:22 开发 后端 本文介绍了Jython的操作符重载,并提供了代码实例。

Jython的操作符重载

像 C++ 一样,但是与 Java 语言不同,Jython 允许类重载许多标准语言操作符。这意味着类可以为语言操作符定义特定的意义。 Jython 还允许类模仿内置类型,如数字、序列和映射。

在下面的例子中,我们将使用标准 Jython UserList 类定义展示实际的操作符重载的例子.UserList 是一个包装了一个列表的类,它的行为也像列表。它的大多数函数都 指派(传递)给其包含的列表,称为data。在一个更实际的Jython操作符重载的例子中,会实现这些重载的函数以访问其他一些存储,如磁盘文件或者数据库。

  1. classUserList:
  2. def__init__(self,initlist=None):
  3. self.data=[]
  4. ifinitlistisnotNone:
  5. iftype(initlist)==type(self.data):
  6. self.data[:]=initlist
  7. elifisinstance(initlist,UserList):
  8. self.data[:]=initlist.data[:]
  9. else:
  10. self.data=list(initlist)
  11. def__cast(self,other):
  12. ifisinstance(other,UserList):returnother.data
  13. else:returnother
  14. #`self`,repr(self)
  15. def__repr__(self):returnrepr(self.data)
  16. #self<other
  17. def__lt__(self,other):returnself搬迁户头是怎么分配.data<self.__cast(other)
  18. #self<=other
  19. def__le__(self,other):returnself.data<=self.__cast(other)
  20. #self==other
  21. def__eq__(self,other):returnself.data==self.__cast(other)
  22. #self!=other,self<>other
  23. def__ne__(self,other):returnself.data!=self.__cast(other)
  24. #self>other
  25. def__gt__(self,other):returnself.data>self.__cast(other)
  26. #self>=other
  27. def__ge__(self,other):returnself.data>=self.__cast(other)
  28. #cmp(self,other)
  29. def__cmp__(self,other):
  30. raiseRuntimeError,"UserList.__cmp__()isobsolete"
  31. #iteminself
  32. def__contains__(self,item):returniteminself.data
  33. #len(self)
  34. def__len__(self):returnlen(self.data)
  35. #self[i]
  36. def__getitem__(self,i):returnself.data[i]
  37. #self[i]=item
  38. def__setitem__(self,i,item):self大棚建房可以强拆吗.data[i]=item
  39. #delself[i]
  40. def__delitem__(self,i):delself.data[i]
  41. #self[i:j]
  42. def__getslice__(self,i,j):
  43. i=max(i,0);j=max(j,0)
  44. returnself.__class__(self.data[i:j])
  45. #self[i:j]=other
  46. def__setslice__(self,i,j,other):
  47. i=max(i,0);j=max(j,0)
  48. ifisinstance(other,UserList):
  49. self.data[i:j]=other.data
  50. elifisinstance(other,type(self.data)):
  51. self.data[i:j]=other
  52. else:
  53. self.data[i:j]=list(other)
  54. #delself[i:j]
  55. def__delslice__(self,i,j):
  56. i=max(i,0);j=max(j,0)
  57. delself.data[i:j]
  58. #self+other(join)
  59. def__add__(self,other):
  60. ifisinstance(other,UserList):
  61. returnself.__class__(self.data+other.data)
  62. elifisinstance(other,type(self.data)):
  63. returnself.__class__(self.data+other)
  64. else:
  65. returnself.__class__(self.data+list(other))
  66. #other+self(join)
  67. def__radd__(self,other):
  68. ifisinstance(other,UserList):
  69. returnself.__class__(other.data+self.data)
  70. elifisinstance(other,type(self.data)):
  71. returnself.__class__(other+self.data)
  72. else:
  73. returnself.__class__(list(other)+self.data)
  74. #self+=other(join)
  75. def__iadd__(self,other):
  76. ifisinstance(other,UserList):
  77. self.data+=other.data
  78. elifisinstance(other,type(self.data)):
  79. self.data+=other
  80. else:
  81. self.data+=list(other)
  82. returnself
  83. #self*other(repeat)
  84. def__mul__(self,n):
  85. returnself.__class__(self.data*n)
  86. __rmul__=__mul__
  87. #self*=other(repeat)
  88. def__imul__(self,n):
  89. self.data*=n
  90. returnself
  91. #implement"List"functionsbelow:
  92. defappend(self,item):self.data.append(item)
  93. definsert(self,i,item):self.data.insert(i,item)
  94. defpop(self,i=-1):returnself.data.pop(i)
  95. defremove(self拆迁房屋面积怎么算,item):self.data.remove(item)
  96. defcount(self,item):returnself.data.count(item)
  97. defindex(self,item):returnself.data.index(item)
  98. defreverse(self):self.data.reverse()
  99. defsort(self,*args):apply(self.data.sort,args)
  100. defextend(self,other):
  101. ifisinstance(other,UserList):
  102. self.data.extend(other.data)
  103. else:
  104. self.data.extend(other)

以上就是Jython的操作符重载的一个实例。

【编辑推荐】

  1. 与Java相比Jython性能表现
  2. 在代码中深入学习Jython语法
  3. 在Eclipse下配置Jython的简易流程
  4. 使用Jython脚本管理WebSphere资源
  5. 如何在Java中调用Jython