我有非常简单的java代码:
public class MessageListenerExample extends ListenerAdapter
{
@Override
public void onMessageReceived(MessageReceivedEvent event)
{
// do something with event
}
}
但是,我似乎无法理解如何将该代码转换为 clojure 代码。文档和文章非常混乱。我也很高兴看到更多例子。我也有兴趣使用implements
.
您可以使用proxy http://clojuredocs.org/clojure.core/proxy扩展现有的 Java 类并实现接口。例如:
(import '[java.awt.event ActionListener ActionEvent KeyAdapter KeyEvent])
(def listener
(proxy
;; first vector contains superclass and interfaces that the created class should extend/implement
[KeyAdapter ActionListener]
;; second vector contains arguments to superclass constructor
[]
;; below are all overriden/implemented methods
(keyPressed [event]
(println "Key pressed" event))
(actionPerformed [action]
(println "Action performed" action))))
(.keyPressed listener nil)
;; => Key pressed nil
(.actionPerformed listener nil)
;; => Action performed nil
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)