我如何使用克隆技术制造以下物体? 物体取自 j(From Effective Java):
NutritionFacts cocaCola =
new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();
我如何使用克隆技术制造以下物体? 物体取自 j(From Effective Java):
NutritionFacts cocaCola =
new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();
虽然很难用其他答复中的分数表示:“......” 1 /sup” ,但“条形” 。 我个人希望:
(-> (NutritionFacts$Builder. 240 8)
(.calories 100)
(.sodium 350)
(.carbohydrates 27)
(.build))
这有两个特点:
.
is right there.最重要的是,对这一问题的所有其他答复都夸大了等级错误: Java s NutritionFacts. 食糖是真正称为“营养食品”的JV级体内的语言糖,而这一类是一种服装必须指的(因为我们没有使用javac来汇编我们的代码)。
<>1> 我不同意<代码>doto的建议:该建议之所以可行,只是因为这一建筑商的班子会以一个单一场合的突变实施其方法链,然后返回。 doto
对 Java物体而言是巨大的,这些物体需要内部的突变,但如果一个类别足以预示其可变性,则真正应当使用方法链式版本(例如,->
)。
http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/.rel=“nofollow”>。 它分两条。 这只允许你需要做些什么,即根据以前的结果,连续使用下 Java法。
我没有再保留地,但你的行文应当转化为:
(.. (NutritionFacts.Builder. 240 8)
(calories 100)
(sodium 350)
(carbohydrates 27)
(build))
我只从克隆开始,但像标准方法一样在我身上援引:
(doto
(NutritionFacts.Builder. 240 8)
(.carbohydrates 27)
(.sodium 35)
(.calories 100)
(.build)
)
EDIT:
As @Goran Jovic points out, this calls all methods on the object created in the first form.
So it works in this case since the Java code uses method chaining, but is not more generally applicable.
www.un.org/chinese/ga/president (NutrionalFacts.Builder. 240 8) (calories 100)(35) (carbohydrates 27) (build)
This thread is a couple of years old, and the thread-first macro is still preferred way as far as i can tell, but the $ syntax is not needed. The slash (/) works as well:
(-> (Caffeine/newBuilder)
(.maximumSize 10000)
(.build))
I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...
I m using Clojure and I need to run a small simulation. I have a vector of length n (n is usually between 10 and 100) that holds values. On each simulation round (maybe 1000 rounds together), one of ...
I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...
I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...
OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...
All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...
I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...
Let s say I have a LazySeq of java.lang.Character like ( ! / \% 1 9 / . i \% $ i space ^@) How can I convert this to a String? I ve tried the obvious (String. my-char-seq) but ...