Clojure:在Java对象上调用一系列方法

3

我曾经在某个地方看到过这个文档,但是我现在不记得它在哪里以及函数的名称是什么:我正在寻找的是一个函数/宏,它将一个Java对象作为参数,对该对象执行一系列的方法,并返回它。类似于这样的东西:

(<the function> obj
  (.setName obj "the name")
  (.setAmount obj42.0)
  ; ...
  (.setDescription obj "the description"))  ; returns the updated obj
1个回答

5
您可以使用..
(.. obj (setName "the name") (setAmount 42.0) ... (setDescription "the description"))

如果方法没有返回目标对象,您可以使用 doto:
(doto obj (.setName "the name") (.setAmount 42.0) ... (.setDescription "the description"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接