【问题标题】:Clojure to Java InteropClojure 到 Java 互操作
【发布时间】:2012-07-27 02:55:22
【问题描述】:

我正在尝试让 javafx2 与 Clojure 一起工作 - 在实现诸如 DoubleBinding 之类的抽象类时,我不确定在 Clojure 中 super.bind(moo) 的等价物是什么。我正在实现的类可以在这里找到:http://docs.oracle.com/javafx/2/api/index.html

(def moo (ObservableDoubleValue. ...))
(def foo (proxy [DoubleBinding] []
            (computeValue []
               (Math/sqrt (.getValue moo)))))



final ObservableDoubleValue moo = ...;   
DoubleBinding foo = new DoubleBinding() {
     {
         super.bind(moo);
     }

     @Override
     protected double computeValue() {
         return Math.sqrt(moo.getValue());
     }
 };

【问题讨论】:

    标签: clojure


    【解决方案1】:

    根据proxy documentation,代理中的方法无法访问super...我建议您使用gen-class 生成类并使用它。如果您使用:exposes-methods 指令公开它们,您可以访问super 的方法。比如:

    (gen-class :name MyDoubleBinding
               :extends DoubleBinding
               :exposes-methods {bind my-bind}
     ....
     )
    

    然后从您的构造函数中调用-my-bind...

    请查看 Clojure 网站上的 documentation about class generation,了解有关 gen-class 的更多详细信息

    【讨论】:

    • proxy-super 呢?
    • 我尝试过使用proxy-super,但是从我测试和阅读的内容来看,它必须从代理的方法内部调用。我一直在测试 gen-class 并且无法在 post-init 构造函数中调用 .bindSuper ,但我仍然尝试 - 因为 gen-class 每次更改都需要重新编译,所以速度很慢。
    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多