Jython的操作符重载实例
创始人
2024-04-15 06:10:40
0

Jython的操作符重载

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

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

  1. class UserList:  
  2.     def __init__(self, initlist=None):  
  3.         self.data = []  
  4.         if initlist is not None:  
  5.             if   type(initlist) == type(self.data):  
  6.                 self.data[:] = initlist  
  7.             elif isinstance(initlist, UserList):  
  8.                 self.data[:] = initlist.data[:]  
  9.             else:  
  10.                 self.data = list(initlist)  
  11.  
  12.     def __cast(self, other):  
  13.         if isinstance(other, UserList): return other.data  
  14.         else:                           return other  
  15.  
  16.     #  `self`, repr(self)  
  17.     def __repr__(self): return repr(self.data)  
  18.  
  19.     #  self < other  
  20.     def __lt__(self, other): return self.data <  self.__cast(other)  
  21.  
  22.     #  self <= other  
  23.     def __le__(self, other): return self.data <= self.__cast(other)  
  24.  
  25.     #  self == other  
  26.     def __eq__(self, other): return self.data == self.__cast(other)  
  27.  
  28.     #  self != other, self <> other  
  29.     def __ne__(self, other): return self.data != self.__cast(other)  
  30.  
  31.     #  self > other  
  32.     def __gt__(self, other): return self.data >  self.__cast(other)  
  33.  
  34.     #  self >= other  
  35.     def __ge__(self, other): return self.data >= self.__cast(other)  
  36.  
  37.     #  cmp(self, other)  
  38.     def __cmp__(self, other):  
  39.         raise RuntimeError, "UserList.__cmp__() is obsolete" 
  40.  
  41.     #  item in self  
  42.     def __contains__(self, item): return item in self.data  
  43.  
  44.     #  len(self)  
  45.     def __len__(self): return len(self.data)  
  46.  
  47.     #  self[i]  
  48.     def __getitem__(self, i): return self.data[i]  
  49.  
  50.     #  self[i] = item  
  51.     def __setitem__(self, i, item): self.data[i] = item  
  52.  
  53.     #  del self[i]  
  54.     def __delitem__(self, i): del self.data[i]  
  55.  
  56.     #  self[i:j]  
  57.     def __getslice__(self, i, j):  
  58.         i = max(i, 0); j = max(j, 0)  
  59.         return self.__class__(self.data[i:j])  
  60.  
  61.     #  self[i:j] = other  
  62.     def __setslice__(self, i, j, other):  
  63.         i = max(i, 0); j = max(j, 0)  
  64.         if   isinstance(other, UserList):  
  65.             self.data[i:j] = other.data  
  66.         elif isinstance(other, type(self.data)):  
  67.             self.data[i:j] = other  
  68.         else:  
  69.             self.data[i:j] = list(other)  
  70.  
  71.     #  del self[i:j]  
  72.     def __delslice__(self, i, j):  
  73.         i = max(i, 0); j = max(j, 0)  
  74.         del self.data[i:j]  
  75.  
  76.     #  self + other   (join)  
  77.     def __add__(self, other):  
  78.         if   isinstance(other, UserList):  
  79.             return self.__class__(self.data + other.data)  
  80.         elif isinstance(other, type(self.data)):  
  81.             return self.__class__(self.data + other)  
  82.         else:  
  83.             return self.__class__(self.data + list(other))  
  84.  
  85.     #  other + self   (join)  
  86.     def __radd__(self, other):  
  87.         if   isinstance(other, UserList):  
  88.             return self.__class__(other.data + self.data)  
  89.         elif isinstance(other, type(self.data)):  
  90.             return self.__class__(other + self.data)  
  91.         else:  
  92.             return self.__class__(list(other) + self.data)  
  93.  
  94.     #  self += other  (join)  
  95.     def __iadd__(self, other):  
  96.         if   isinstance(other, UserList):  
  97.             self.data += other.data  
  98.         elif isinstance(other, type(self.data)):  
  99.             self.data += other  
  100.         else:  
  101.             self.data += list(other)  
  102.         return self 
  103.  
  104.     #  self * other   (repeat)  
  105.     def __mul__(self, n):  
  106.         return self.__class__(self.data*n)  
  107.     __rmul__ = __mul__  
  108.  
  109.     #  self *= other  (repeat)  
  110.     def __imul__(self, n):  
  111.         self.data *= n  
  112.         return self 
  113.  
  114.     # implement "List" functions below:  
  115.  
  116.     def append(self, item): self.data.append(item)  
  117.  
  118.     def insert(self, i, item): self.data.insert(i, item)  
  119.  
  120.     def pop(self, i=-1): return self.data.pop(i)  
  121.  
  122.     def remove(self, item): self.data.remove(item)  
  123.  
  124.     def count(self, item): return self.data.count(item)  
  125.  
  126.     def index(self, item): return self.data.index(item)  
  127.  
  128.     def reverse(self): self.data.reverse()  
  129.  
  130.     def sort(self, *args): apply(self.data.sort, args)  
  131.  
  132.     def extend(self, other):  
  133.         if isinstance(other, UserList):  
  134.             self.data.extend(other.data)  
  135.         else:  
  136.             self.data.extend(other)  
  137.  

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

【编辑推荐】

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

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...