Clojure:我如何需要一个类并调用静态方法? [英] Clojure: how do I require a class and call a static method?
问题描述
只是进入Clojure,我在命名空间,路径和类语法上遇到了一些麻烦。
Just getting into Clojure, and I'm having a little trouble with namespaces, paths and class syntax.
我首先测试并编写了一个简单的hello world函数
I started by testing and writing a simple hello world function.
test / test_subject_test.clj:
test/test_subject_test.clj:
(ns test-subject-test
(:require [clojure.test :refer :all]
[test-subject :refer :all]))
(deftest testit
(testing "All good?"
(is (= "I got this" (subject))))
src / test_subject.clj:
src/test_subject.clj:
(ns test-subject)
(defn subject []
"I got this")
一切正常,所以我决定尝试在下一个类上调用静态方法。
That all works fine, so I decided to try calling a static method on a class next.
test / test_subject_test.clj:
test/test_subject_test.clj:
(ns test-subject-test
(:require [clojure.test :refer :all]
[test-subject :refer :all]
[hello :refer :all]))
(deftest testit
(testing "All good?"
(is (= "I got this" (subject))))
(testing "Try calling a static method"
(is (= "Hello Guy!" (-handler Hello "Guy")))))
src / hello.clj:
src/hello.clj:
(ns hello
(:gen-class :name "Hello"
:methods [^:static [handler [String] String]]))
(defn -handler [s]
(str "Hello " s "!"))
但是现在编译器抛出一条带有一条有趣消息的长堆栈跟踪:由以下原因引起:java.lang.RuntimeException:无法解析符号:在这种情况下,您好
。我已经尝试过:require
语句和(-handler Hello)
调用的一些排列,尝试不使用:refer:all
并将该方法称为(-handler(.Hello hello))
我缺少有关类在Clojure中如何工作的真正基础知识。
But now the compiler throws a long stack trace with one interesting message: Caused by: java.lang.RuntimeException: Unable to resolve symbol: Hello in this context
. I've tried a few permutations of the :require
statement and the (-handler Hello)
call, I've tried not using :refer :all
and referring to the method as (-handler (.Hello hello))
but clearly I'm missing something really basic about how classes work in Clojure.
我查看了 Clojure vars和Java静态方法,但是对于我要执行的操作来说,它似乎比必需的方法复杂得多:只需调用一个静态方法即可。但是,这个问题中有一个有趣的引号,使我尝试这样做:(Hello处理程序 Guy)
没有成功(无法解析符号:在这种情况下,您好)。
I had a look at Clojure vars and Java static methods, but it seems way more complex than necessary for what I am trying to do: just call a static method. However, there was one interesting quote in the question, which led me to try this: (Hello handler "Guy")
without success (Unable to resolve symbol: Hello in this context).
那么,我应该如何从另一个文件中请求一个类并对其调用静态方法?
So, how should I require a class from another file and call a static method on it?
推荐答案
如果您将一个类编译到默认包中(这就是您在此处所做的操作),那么所有这些都会稍有不同,因为您无法导入这些类。我不建议您这样做。在下面的代码中,我假装您实际上写了(:gen-class:name hello.Hello ...)
All this is slightly different if you're compiling a class into the default package (which is what you're doing here) since you can't import those classes. I don't recommend you do that. In the following code I'm pretending you actually wrote (:gen-class :name "hello.Hello" ...)
要在Java类上调用方法,您应该使用完全限定的类名或首先使用 import
该类:
To call methods on a java class you should either use the fully qualified classname or first import
the class:
(import 'hello.Hello)
(Hello/handler "there")
或
(hello.Hello/handler "there")
如您所见,您应该使用/调用Java类上的静态方法。
As you can see, you should use / to call static methods on java classes.
要使所有这些与gen-class一起使用,必须确保首先编译这些类。确保 的最简单方法是先 require
其名称空间:
For this all to work with gen-class you must make sure that the classes are compiled first. The simplest way to guarantee that is to require
its namespace first:
(require 'hello)
(import 'hello.Hello)
因为这比使用普通Clojure函数&命名空间,如果您只想从clojure调用代码,则可能不应该使用Java interop。
Because this is all more trouble than using plain clojure functions & namespaces, you probably shouldn't be using java interop if you're only intending to call the code from clojure.
这篇关于Clojure:我如何需要一个类并调用静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!