【问题标题】:Clojure: calling a sequence of methods on a Java objectClojure:在 Java 对象上调用一系列方法
【发布时间】:2018-09-12 13:37:09
【问题描述】:

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

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

【问题讨论】:

    标签: clojure clojure-java-interop


    【解决方案1】:

    你可以使用..:

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

    如果方法没有返回目标对象,你可以使用doto:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多