首页 > 解决方案 > Clojure:在 Java 对象上调用一系列方法

问题描述

我在某个地方看到过这个文档,但我不记得函数的名称和名称是什么:我正在搜索的是一个函数/宏,它以(Java)对象作为参数,在该对象上执行一系列方法和返回它。像这样的东西:

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

标签: clojureclojure-java-interop

解决方案


您可以使用..

(.. obj (setName "the name") (setAmount 42.0) ... (setDescription "the description"))

如果方法不返回目标对象,您可以使用doto

(doto obj (.setName "the name") (.setAmount 42.0) ... (.setDescription "the description"))

推荐阅读