使用代理设置继承的实例变量?

时间:2015-05-05 18:01:04

标签: clojure

我知道可以使用gen-class关键字在Clojure中使用:exposes设置从Java类继承的实例变量。有没有办法使用proxy设置继承的实例变量?

我在Clojure中重写了一些Java代码。 Java源代码使用匿名扩展类,因此尝试在Clojure版本中使用proxy似乎很自然。这是Java版本(简化):

yardPortrayal.setPortrayalForAll(new OvalPortrayal2D(){
    public void draw(Object object, Graphics2D graphics, DrawInfo2D info) {
        Student student = (Student)object;
        int agitationShade = (int) (student.getAgitation() * 255 / 10.0);
        paint = new Color(agitationShade, 0, 255 - agitationShade);
        super.draw(object, graphics, info);
    }
});

paint是超类中的变量,需要在调用super.draw()之前设置它。

这是我的Clojure版本:

(.setPortrayalForAll yard-portrayal
                     (proxy [OvalPortrayal2D] []
                       (draw [student graphics info]
                         (let [agitation-shade (int (/ (* 255.0 (.getAgitation student)) 10.0))
                               color (Color. agitation-shade 0 (- 255 agitation-shade))]
                           ;; HOW TO SET INHERITED paint VAR TO color?
                           (proxy-super draw student graphics info)))))

0 个答案:

没有答案