gpt4 book ai didi

clojure - Clojure 中的 Java,评估问题

转载 作者:行者123 更新时间:2023-12-02 15:17:43 26 4
gpt4 key购买 nike

对于在 REPL 上评估以下内容所获得的结果是否有详细解释。

(.PI Math)

给出

IllegalArgument Exception

同时

(. Math PI)

评估为

3.141592653589793

最佳答案

解释在http://clojure.org/java_interop .

user> (macroexpand '(.PI Math))
(. (clojure.core/identity Math) PI)

(identity Math) 返回表示 Math 类的 Class 对象。您尝试访问此 Class 对象中名为 PI 的实例成员,但它不存在。 (这与访问 Math 类中名为 PI 的静态成员不同。)您只能使用此 Class 对象进行反射,或者将类作为对象传递给其他方法,或者类似的东西。

user> (class (identity Math))
java.lang.Class
user> (.getName (identity Math))
"java.lang.Math"
user> (.getName Math)
"java.lang.Math"
user> (.getMethods Math)
#<Method[] [Ljava.lang.reflect.Method;@12344e8>
user> (vec (.getMethods Math))
[#<Method public static int java.lang.Math.abs(int)> #<Method public static long java.lang.Math.abs(long)> #<Method public static float java.lang.Math.abs(float)> ...]
user> (.getField Math "PI")
#<Field public static final double java.lang.Math.PI>
user> (.getDouble (.getField Math "PI") Math)
3.141592653589793

做你想做的事情的最短方法可能是数学/PI

user> (macroexpand '(Math/PI))
(. Math PI)
user> Math/PI
3.141592653589793
user> (. Math PI)
3.141592653589793

关于clojure - Clojure 中的 Java,评估问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2268213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com