Jython高级类特性深度解析
在Jython编程中,特殊属性和方法是创建高度定制化对象的关键。这些特殊属性和方法以双下划线开头和结尾,为对象赋予了复杂的行为。下面将深入探讨Jython高级类的各个方面。
1. 预定义类属性
类和实例隐式拥有一些特殊属性,这些属性在类定义执行或实例创建时自动出现。Jython将Java类和实例进行包装,使其也具有特殊属性。Jython类有五个特殊属性,而Java类有其中三个。这些属性均可读,但只有部分可赋值。
为了更好地理解这些特殊属性,我们定义一个简单的Jython类 LineDatum
:
# file: datum.py
import java
class LineDatum(java.util.Hashtable):
"""Collects points that lie on a line.
Instantiate with line slope and intercept: e.g. LineDatum(.5, 3)"""
def __init__(self, slope, incpt):
self.slope = slope
self.incpt = incpt
def addPoint(self, x, y):
"""addPoint(x, y) – > 1 or 0
Accepts coordinates for a cartesian point (x,y). If point is