将字段添加到Clojure中的一个代理的类
题
我使用“代理”在一个Clojure的GUI应用程序扩展各种Swing类,一般用的代码看起来是这样的:
(def ^JPanel mypanel
(proxy [JPanel] []
(paintComponent [#^Graphics g]
(.drawImage g background-image 0 0 nil))))
这工作得很好,但我无法弄清楚如何添加其他字段到新扩展类,例如,使背景图像可能被随后更新的字段。这将是在Java中很容易和普遍的做法。
有没有好办法Clojure中做到这一点?或者是有以实现相同效果的另一优选的方法?
解决方案
您可以使用这样的:
(defn ^JPanel mypanel [state]
(proxy [JPanel] []
(paintComponent [#^Graphics g]
(do
(comment do something with state)
(.drawImage g background-image 0 0 nil)))))
或使用任何其他的外部函数/参考文献
不隶属于 StackOverflow