无法理解:Clojure 中的状态
我明白什么:声明/做什么/。 它会在您的类中创建一个字段,就像在 Java 中一样。 我不明白的是这有什么意义? 似乎我只看到它是通过 Clojure 生成的扩展其他类的类来完成的。 http://www.fatvat.co.uk/2009/ 05/clojure-and-robocode.html 就是一个例子。 我不懂Java,也不太精通面向对象编程。 有人可以向我解释一下 :state 的意义,以及它与 Java 互操作的契合点吗?
多谢!
注意:当我说 :state 时,我指的是 (:gen-class :state)
I understand what :state /does/. It creates a field, like in Java, in your class. What I don't understand is what is the point of this? It seems like I only see it done with Clojure-generated classes that extend other classes. http://www.fatvat.co.uk/2009/05/clojure-and-robocode.html being one example. I don't know Java, and I'm not very well versed in Object Oriented Programming. Can someone explain the point of :state to me, and where it all fits in with Java interop?
Thanks a lot!
NOTE: When I say :state, I am referring to (:gen-class :state)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
:state 只是在作为 gen 类一部分生成的函数之间共享某些数据的一种方式。 将其视为与对象的实例数据完全相同。
:state is simply a way of sharing some data between the functions generated as part of gen-class. Think of it as being exactly the same as the instance data of an object.
有关
state
以及如何初始化它的更多信息,请参阅文章 gen-class – 它如何工作以及如何使用它来自文章:
:state
定义了一个返回对象状态的方法。:init
定义初始化程序的名称。 这是一个必须返回向量的函数。 第一个元素又是超类构造函数的参数向量。 在我们的例子中,这只是空向量。 第二个元素是对象的状态。总之,
init
返回对象的状态,并在实例化对象时调用。state
是类上的一种方法,而不是函数,它返回的值与init
返回的向量中的第二个元素相同。然后,本文继续展示如何使用原子来更改对象的状态(如果需要)。
More infomation on the
state
and how to initialize it can be found the article gen-class – how it works and how to use itFrom the article:
:state
defines a method which will return the object's state.:init
defines the name of the initialiser. This is a function which has to return a vector. The first element is again a vector of arguments to the super class constructor. In our case this is just the empty vector. The second element is the object's state.In summary,
init
returns the state of the object and is called when the object is instantiated.state
is a method on the class, as opposed to a function, that will return the same value returned as the second element in the vector returned byinit
.The article then goes on to show how to use an atom to be able to change the state of the object, if needed.
我在#Clojure IRC 频道上与hiredman 讨论过这个问题,他告诉我,它的要点是每个实例的状态。 这就说得通了。
I talked it over with hiredman on the #Clojure IRC channel, and he told me that the main point of it is a state per instance. That makes sense.