我想在类的方法中使用通过gen类构造的类的实例。
我怎样才能访问它?在以下示例中,我为"this“插入了哪些内容:
(ns example
(:gen-class))
(defn -exampleMethod []
(println (str this)))或者是不可能使用gen-class?
发布于 2015-04-29 11:25:20
Clojure函数的第一个参数对应于gen类生成的方法,它接受正在调用其方法的当前对象。
(defn -exampleMethod [this]
(println (str this)))此外,在定义来自生成类的超类或接口的方法时,还必须向gen-class添加一个gen-class选项。因此,一个完整的例子如下。
example.clj
(ns example)
(gen-class
:name com.example.Example
:methods [[exampleMethod [] void]])
(defn- -exampleMethod
[this]
(println (str this)))REPL
user> (compile 'example)
example
user> (.exampleMethod (com.example.Example.))
com.example.Example@73715410
nilhttps://stackoverflow.com/questions/29941506
复制相似问题