我拼命地试图创建一个使用来自clojure的"toString"方法的类 根据clojure文档,以下内容应该有效:
(ns override-test.simpleClass
(:gen-class
:name simpleClass
:methods [[^{Override {}} toString [] String]]
:state state
:init init
:constructors {[String] []}))
(defn -init
[name_]
[[] (atom name_)])
(defn -toString [this]
(deref (.state this)))
然而评估
(simpleClass. "test")
投掷CompilerException java.lang.ClassFormatError: Duplicate method name "toString" with signature "()Ljava.lang.String;" in class file simpleClass, compiling:(override_test/simpleClass.clj:19:3)
任何煽动我可能做错了什么?
正如Biped Pill所提到的,问题在于toString似乎已经通过gen-class的自动子类化机制实现了。可能是Ljava.lang。字符串被视为(?(超类,并且自动添加toString,所以我只需要将其从:methods中删除,这是针对非继承方法的,它就像一个魅力。