Jython高级类特性详解
1. 前置类属性与实例属性
在Jython中,类和实例自带一些特殊属性,当类定义执行或实例创建时,这些属性会自动出现。Jython类有五个特殊属性,而Java类有其中三个。所有这些属性都可读,但只有部分可赋值。
1.1 类属性示例
为了探究这些特殊属性,我们定义一个简单的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
on the line, it adds the point to the instance."""
if y == self.slope * x + self.in