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

问题描述 投票:3回答:1

我已经在某处记录了这个,但是我不记得函数的名称在哪里和是什么:我正在搜索的是一个函数/宏,它将(一个Java)对象作为参数,在该对象上执行一系列方法归还它。像这样的东西:

(<the function> obj
  (.setName obj "the name")
  (.setAmount obj42.0)
  ; ...
  (.setDescription obj "the description"))  ; returns the updated obj
clojure clojure-java-interop
1个回答
4
投票

你可以使用..

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

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

(doto obj (.setName "the name") (.setAmount 42.0) ... (.setDescription "the description"))
© www.soinside.com 2019 - 2024. All rights reserved.