成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Jython的操作符重載實(shí)例

開發(fā) 后端
本文介紹了Jython的操作符重載,并提供了代碼實(shí)例。

Jython的操作符重載

像 C++ 一樣,但是與 Java 語(yǔ)言不同,Jython 允許類重載許多標(biāo)準(zhǔn)語(yǔ)言操作符。這意味著類可以為語(yǔ)言操作符定義特定的意義。 Jython 還允許類模仿內(nèi)置類型,如數(shù)字、序列和映射。

在下面的例子中,我們將使用標(biāo)準(zhǔn) Jython UserList 類定義展示實(shí)際的操作符重載的例子.UserList 是一個(gè)包裝了一個(gè)列表的類,它的行為也像列表。它的大多數(shù)函數(shù)都 指派(傳遞)給其包含的列表,稱為data。在一個(gè)更實(shí)際的Jython操作符重載的例子中,會(huì)實(shí)現(xiàn)這些重載的函數(shù)以訪問(wèn)其他一些存儲(chǔ),如磁盤文件或者數(shù)據(jù)庫(kù)。

  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的操作符重載的一個(gè)實(shí)例。

【編輯推薦】

  1. 與Java相比Jython性能表現(xiàn)
  2. 在代碼中深入學(xué)習(xí)Jython語(yǔ)法
  3. 在Eclipse下配置Jython的簡(jiǎn)易流程
  4. 使用Jython腳本管理WebSphere資源
  5. 如何在Java中調(diào)用Jython
責(zé)任編輯:yangsai 來(lái)源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

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#操作符重載應(yīng)用

2021-10-31 18:59:55

Python操作符用法

2009-07-21 09:31:00

Scala操作符

2010-07-14 14:55:07

Perl操作符

2009-08-19 17:26:28

C# 操作符

2010-07-13 11:11:39

Perl標(biāo)量

2009-11-30 16:48:08

PHP操作符

2011-04-08 16:26:14

JavaScript

2010-07-14 14:30:31

Perl操作符

2010-07-19 11:00:24

Perl操作符

2014-01-14 10:22:21

LinuxLinux命令

2009-09-15 17:16:58

LINQ查詢操作符

2012-02-06 09:13:23

LINQ

2009-09-16 09:09:23

Linq Contai
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 黑人巨大精品欧美一区二区免费 | 欧美日产国产成人免费图片 | 久久99蜜桃综合影院免费观看 | 欧美日韩精品久久久免费观看 | 精品免费国产一区二区三区四区 | 二区av | 久久久久久久一区 | 天天影视亚洲综合网 | 中文字幕精品一区二区三区在线 | 91网站视频在线观看 | 久久久美女 | 亚洲综合无码一区二区 | 一区二区三区国产好的精 | 久久男女视频 | 亚洲欧美日韩精品 | 中文字幕第一页在线 | 成人在线国产 | 日韩欧美三级 | 中文字幕一区二区不卡 | 国产亚洲精品a | 国产高清精品在线 | 欧美一级做性受免费大片免费 | 伊人啪啪网| 国产在线不卡 | 四虎影 | 中文在线播放 | av日韩在线播放 | 亚洲精品一区在线观看 | 午夜影院网站 | 国产成人精品一区二区在线 | 日韩资源 | 亚洲日本免费 | 毛片在线视频 | 亚洲免费在线观看视频 | 欧美色欧美亚洲另类七区 | 日日夜夜天天 | 天天碰夜夜操 | 午夜影院在线 | 国产偷录叫床高潮录音 | 一区中文字幕 | 国产精品永久在线观看 |