Clojure 如何从导入类创建新对象

Clojure 如何从导入类创建新对象,clojure,Clojure,我已导入firestore快照并尝试创建其对象 (:import [com.google.cloud.firestore QueryDocumentSnapshot]) (def snapshot1 (QueryDocumentSnapshot.toObject. [:reference "user1" :type "Promotion" :included-scans 100])) 但编译失败,错误为: Exception in thread "main" java.lang

我已导入firestore快照并尝试创建其对象

(:import [com.google.cloud.firestore
        QueryDocumentSnapshot])
(def snapshot1 (QueryDocumentSnapshot.toObject. [:reference "user1" :type "Promotion" :included-scans 100]))
但编译失败,错误为:

Exception in thread "main" java.lang.ClassNotFoundException: QueryDocumentSnapshot.toObject,

你能帮我为这个类创建一个新对象吗?

你可以调用一个构造函数,比如

(QueryDocumentSnapshot.whatever参数)

但是QueryDocumentSnapshot没有公共构造函数,只能使用如下静态工厂方法对其进行实例化:

(QueryDocumentSnapshot/fromDocument firestore时间戳文档)


我不确定你到底想在这门课上取得什么成绩,但看起来你对那门课做不到你认为你能做的。

好吧,在考虑了你和Joost的答案后,我发现了一些相当有趣的事情:

错误消息显示,找不到类
QueryDocumentSnapshot.toObject
。给你

如果要调用静态方法,必须编写
(类/方法参数)

有关java互操作的更多信息,我强烈推荐官方文档:


另外,还要考虑Joost对类本身的注释。

我打赌您的导入语句必须包含在名称空间声明中。像
(ns一些名称空间(:import com.google.cloud.forestore.QueryDocumentSnapshot))
@Joshua是的,导入绝对在名称空间内。嗨,我想我们可以使用它的公共方法“toObject”。但您能建议如何为该类创建对象吗?
toObject
是实例方法。您必须使用
fromDocument
工厂方法创建
QueryDocumentSnapshot
,然后在其上调用
toObject
(.toObject(QueryDocumentSnapshot/fromdocumentfirestore timestamp document))谢谢@AlešRoubíček,但是我没有要放入的firestore store对象。我正在尝试从hashmap创建QueryDocumentSnapshot对象。也无法调用fromDocument:
线程“main”java.lang.IllegalArgumentException中的异常:没有匹配的方法:fromDocument,编译:
@giahuy您可能没有向fromDocument方法传递正确类型的参数。正如你所说,既然你没有firestore对象,那就可以解释为什么了。我仍然在想你在这里想要实现什么。你在写什么测试吗?