自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Jython的操作符重載實例

開發(fā) 后端
本文介紹了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#操作符重載應用

2021-10-31 18:59:55

Python操作符用法

2010-07-14 14:55:07

Perl操作符

2009-08-19 17:26:28

C# 操作符

2009-07-21 09:31:00

Scala操作符

2010-07-13 11:11:39

Perl標量

2009-11-30 16:48:08

PHP操作符

2011-04-08 16:26:14

JavaScript

2010-07-19 11:00:24

Perl操作符

2010-07-14 14:30:31

Perl操作符

2014-01-14 10:22:21

LinuxLinux命令

2009-09-15 17:16:58

LINQ查詢操作符

2009-09-16 09:09:23

Linq Contai

2010-07-14 14:18:51

Perl操作符
點贊
收藏

51CTO技術棧公眾號