Clojure 使用gen类时如何获取类的实例

Clojure 使用gen类时如何获取类的实例,clojure,Clojure,我想在类的方法中使用通过gen类构造的类的实例 如何访问它?在以下示例中,我为“this”插入了什么内容: (ns example (:gen-class)) (defn -exampleMethod [] (println (str this))) 或者在使用gen类时是不可能的?与gen类生成的方法相对应的Clojure函数的第一个参数采用正在调用其方法的当前对象 (defn -exampleMethod [this] (println (str this))) 除此之外,

我想在类的方法中使用通过gen类构造的类的实例

如何访问它?在以下示例中,我为“this”插入了什么内容:

(ns example
  (:gen-class))

(defn -exampleMethod []
  (println (str this)))

或者在使用gen类时是不可能的?

与gen类生成的方法相对应的Clojure函数的第一个参数采用正在调用其方法的当前对象

(defn -exampleMethod [this]
  (println (str this)))
除此之外,当您定义既不来自超类也不来自所生成类的接口的方法时,必须在
gen class
中添加
:methods
选项。下面是一个完整的例子

example.clj 答复
(ns example)

(gen-class
 :name com.example.Example
 :methods [[exampleMethod [] void]])

(defn- -exampleMethod
  [this]
  (println (str this)))
user> (compile 'example)
example

user> (.exampleMethod (com.example.Example.))
com.example.Example@73715410
nil