本篇內(nèi)容介紹了“Jython的操作符重載”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
匯川網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),匯川網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為匯川近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的匯川做網(wǎng)站的公司定做!
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ǔ),如磁盤(pán)文件或者數(shù)據(jù)庫(kù)。
class UserList: def __init__(self, initlist=None): self.data = [] if initlist is not None: if type(initlist) == type(self.data): self.data[:] = initlist elif isinstance(initlist, UserList): self.data[:] = initlist.data[:] else: self.data = list(initlist) def __cast(self, other): if isinstance(other, UserList): return other.data else: return other # `self`, repr(self) def __repr__(self): return repr(self.data) # self < other def __lt__(self, other): return self.data < self.__cast(other) # self <= other def __le__(self, other): return self.data <= self.__cast(other) # self == other def __eq__(self, other): return self.data == self.__cast(other) # self != other, self <> other def __ne__(self, other): return self.data != self.__cast(other) # self > other def __gt__(self, other): return self.data > self.__cast(other) # self >= other def __ge__(self, other): return self.data >= self.__cast(other) # cmp(self, other) def __cmp__(self, other): raise RuntimeError, "UserList.__cmp__() is obsolete" # item in self def __contains__(self, item): return item in self.data # len(self) def __len__(self): return len(self.data) # self[i] def __getitem__(self, i): return self.data[i] # self[i] = item def __setitem__(self, i, item): self.data[i] = item # del self[i] def __delitem__(self, i): del self.data[i] # self[i:j] def __getslice__(self, i, j): i = max(i, 0); j = max(j, 0) return self.__class__(self.data[i:j]) # self[i:j] = other def __setslice__(self, i, j, other): i = max(i, 0); j = max(j, 0) if isinstance(other, UserList): self.data[i:j] = other.data elif isinstance(other, type(self.data)): self.data[i:j] = other else: self.data[i:j] = list(other) # del self[i:j] def __delslice__(self, i, j): i = max(i, 0); j = max(j, 0) del self.data[i:j] # self + other (join) def __add__(self, other): if isinstance(other, UserList): return self.__class__(self.data + other.data) elif isinstance(other, type(self.data)): return self.__class__(self.data + other) else: return self.__class__(self.data + list(other)) # other + self (join) def __radd__(self, other): if isinstance(other, UserList): return self.__class__(other.data + self.data) elif isinstance(other, type(self.data)): return self.__class__(other + self.data) else: return self.__class__(list(other) + self.data) # self += other (join) def __iadd__(self, other): if isinstance(other, UserList): self.data += other.data elif isinstance(other, type(self.data)): self.data += other else: self.data += list(other) return self # self * other (repeat) def __mul__(self, n): return self.__class__(self.data*n) __rmul__ = __mul__ # self *= other (repeat) def __imul__(self, n): self.data *= n return self # implement "List" functions below: def append(self, item): self.data.append(item) def insert(self, i, item): self.data.insert(i, item) def pop(self, i=-1): return self.data.pop(i) def remove(self, item): self.data.remove(item) def count(self, item): return self.data.count(item) def index(self, item): return self.data.index(item) def reverse(self): self.data.reverse() def sort(self, *args): apply(self.data.sort, args) def extend(self, other): if isinstance(other, UserList): self.data.extend(other.data) else: self.data.extend(other)
“Jython的操作符重載”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
名稱欄目:Jython的操作符重載
當(dāng)前地址:http://muchs.cn/article0/ihdgio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、自適應(yīng)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)