Jython的操作符重载实例

开发 后端
本文介绍了Jython的操作符重载,并提供了代码实例。

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
责任编辑:yangsai 来源: 网络转载
相关推荐

2009-08-18 17:42:12

C#操作符重载

2009-08-18 18:06:54

C#操作符重载

2009-08-18 17:55:20

C#操作符重载

2010-02-05 10:30:02

C++操作符重载

2010-02-03 10:23:47

C++操作符重载

2009-08-18 17:20:17

C#操作符重载

2009-08-18 17:34:25

C#操作符重载应用

2009-07-21 09:31:00

Scala操作符

2009-08-19 17:26:28

C# 操作符

2010-07-14 14:55:07

Perl操作符

2021-10-31 18:59:55

Python操作符用法

2009-11-30 16:48:08

PHP操作符

2010-07-13 11:11:39

Perl标量

2014-01-14 10:22:21

LinuxLinux命令

2009-09-15 17:16:58

LINQ查询操作符

2012-02-06 09:13:23

LINQ

2010-07-14 14:18:51

Perl操作符

2009-09-16 09:09:23

Linq Contai

2011-04-08 16:26:14

JavaScript

2010-07-19 11:00:24

Perl操作符
点赞
收藏

51CTO技术栈公众号