在 Clojure 中动态访问 Java 字段? [英] Access Java fields dynamically in Clojure?

查看:21
本文介绍了在 Clojure 中动态访问 Java 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 clojure 和 java 的新手.

I'm a novice in clojure and java.

为了访问 Clojure 中的 Java 字段,您可以执行以下操作:

In order to access a Java field in Clojure you can do:

Classname/staticField

(. Classname staticField)

(如果我错了,请纠正我)

(correct me if I'm wrong)

当字段名称保存在变量中时,如何访问静态字段?即:

(let [key-stroke 'VK_L
      key-event KeyEvent/key-stroke])

我希望 key-stroke 在尝试访问该字段之前被评估为符号 VK_L.

I want key-stroke to be evaluated into the symbol VK_L before it tries to access the field.

推荐答案

在这种情况下,您可能希望执行以下操作:

In this case you might want to do something like the following:

user=> (def m 'parseInt)          
#'user/m
user=> `(. Integer ~m "10")       
(. java.lang.Integer parseInt "10")
user=> (eval `(. Integer ~m "10"))
10

如果您觉得它有点像黑客,那是因为它确实如此.可能有更好的方法来构建代码,但至少这应该可行.

If you feel like it's a bit hack-ish, it's because it really is such. There might be better ways to structure the code, but at least this should work.

这篇关于在 Clojure 中动态访问 Java 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆