[英]Clojure gen-class this keyword
Is it possible to refer to Java's 'this' keyword from within a gen-class method? 是否可以从gen-class方法中引用Java的'this'关键字?
I am trying to implement daredesm's answer here , in Clojure. 我想实现daredesm的答案在这里 ,Clojure中。 However, when I try to use 'this' in the run function, I get "java.lang.RuntimeException: Unable to resolve symbol: this in this context."
但是,当我尝试在run函数中使用'this'时,出现“ java.lang.RuntimeException:无法解析符号:this in this context”。
(gen-class
:name ClipboardListener
:extends java.lang.Thread
:implements [java.awt.datatransfer.ClipboardOwner]
:prefix ClipboardListener-
:methods [[takeOwnership [Transferable] void]])
(def systemClipboard (.getSystemClipboard (java.awt.Toolkit/getDefaultToolkit)))
(defn ClipboardListener-run []
(let [transferable (.getContents systemClipboard this)]
(.takeOwnership transferable)))
(defn ClipboardListener-lostOwnership [clipboard trasferable] (prn "hit lost"))
(defn ClipboardListener-takeOwnership [transferable] (prn "hit take"))
(defn processClipboard [transferable clipboard] (prn "hit process"))
Note: This is my first time generating Java classes in Clojure, so any general feedback/resources is greatly appreciated. 注意:这是我第一次在Clojure中生成Java类,因此,非常感谢任何常规反馈/资源。
Instance methods can take an implicit 'self' arg- as the first argument. 实例方法可以将隐式“ self” arg-作为第一个参数。 So to take your example:
举个例子:
(defn ClipboardListener-run [this]
(let [transferable (.getContents systemClipboard this)]
(.takeOwnership transferable)))
Note the this
argument :) 注意
this
参数:)
Same goes for any instance method, eg: 任何实例方法都一样,例如:
(defn ClipboardListener-toString [this]
"override Object#toString with something cool")
Have a look at this (no pun intended) for more info on gen-class. 看看这个 (无双关语)以获取有关gen-class的更多信息。
Also consider reify
for cases like Runnable
, Callable
, etc where you just need to implement a small-ish interface. 对于只需要实现小型接口的
Runnable
, Callable
等情况,也可以考虑进行reify
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.