Jython的操作符重载
本篇内容介绍了“Jython的操作符重载”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
汇川网站建设公司创新互联,汇川网站设计制作,有大型网站制作公司丰富经验。已为汇川近1000家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的汇川做网站的公司定做!
Jython的操作符重载
像 C++ 一样,但是与 Java 语言不同,Jython 允许类重载许多标准语言操作符。这意味着类可以为语言操作符定义特定的意义。 Jython 还允许类模仿内置类型,如数字、序列和映射。
在下面的例子中,我们将使用标准 Jython UserList 类定义展示实际的操作符重载的例子.UserList 是一个包装了一个列表的类,它的行为也像列表。它的大多数函数都 指派(传递)给其包含的列表,称为data。在一个更实际的Jython操作符重载的例子中,会实现这些重载的函数以访问其他一些存储,如磁盘文件或者数据库。
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的操作符重载”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
网站题目:Jython的操作符重载
分享URL:http://ybzwz.com/article/ihdgio.html