Edw*_*ard 5 java static static-methods clojure gen-class
在我的Clojure代码中,我想生成一个包含静态方法(命名staticMethod)的类文件,稍后在Java程序的静态上下文中调用该方法.
我试过(Clojure):
(ns com.stackoverflow.clojure.testGenClass
(:gen-class
:name com.stackoverflow.clojure.TestGenClass
:prefix "java-"
:methods [
[#^{:static true} staticMethod [String String] String]
]))
(def ^:private pre "START: ")
(defn #^{:static true} java-staticMethod [this text post]
(str pre text post))
Run Code Online (Sandbox Code Playgroud)
和(Java):
package com.stackoverflow.clojure;
public class TestGenClassTest {
private TestGenClassTest() {
}
public static void main(String[] args) {
TestGenClass.staticMethod("Static call from Java!", " :END");
}
}
Run Code Online (Sandbox Code Playgroud)
在https://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html上我读到:
通过将元数据 - 通过#^ {:static true}添加到方法声明中,您还可以定义静态方法.
无论我把#^{:static true}Java编译器放在哪里,总是说:
无法从TestGenClass类型对静态方法staticMethod(String,String)进行静态引用
如何在Clojure中定义静态方法?会#^{:static true}和^:static意思相同吗?这记录在哪里?
Mar*_*nik 10
当kotka说要注释方法声明时,他"obviosly"意味着整个向量持有声明:
:methods [^:static [staticMethod [String String] String] ]
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种简洁的措辞是Clojure文档的典型代表.