繁体   English   中英

如何使用clojure的Gen-class生成生成静态方法?

[英]How to generate generate static methods with clojure's 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))

和(Java):

package com.stackoverflow.clojure;

public class TestGenClassTest {

    private TestGenClassTest() {
    }

    public static void main(String[] args) {
        TestGenClass.staticMethod("Static call from Java!", " :END");
    }
}

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意味着相同? 这记录在哪里?

当kotka说要注释方法声明时 ,他“obviosly”意味着整个向量持有声明:

:methods [^:static [staticMethod [String String] String] ]

不幸的是,这种简洁的措辞是Clojure文档的典型代表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM