clojure gen-class返回自己的类 [英] clojure gen-class returning own class
问题描述
我现在使用Clojure创建一个类对象,该对象具有返回对象本身的方法。
I'm now making a class object with Clojure which has a method returning the object itself.
使用Java编写,我想创建的对象
Written with Java, the object that I'd like to make is like,
class Point {
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point copy() {
return new Point(this.x, this.y);
}
}
我写的当前clojure代码是, / p>
The current clojure code that I wrote is like,
(ns myclass.Point
:gen-class
:prefix "point-"
:init init
:state state
:constructors {[double double] []}
:methods [[copy [] myclass.Point]]))
(defn point-init [x y]
[[] {:x x :y y}])
(defn point-copy [this]
this)
但是,出现以下错误。
java.lang.ClassNotFoundException: myclass.Point
这个问题,我找不到任何答案。有人知道这个问题的解决方案吗?
While I have googled about this issue, I couldn't find any answers. Does anyone know the solution for this issue?
感谢您的帮助。
推荐答案
我不确定这是唯一的方法,但是为了使用生成的类类型在方法签名,你可以生成该类在2个步骤 - 虽然它仍然在一个文件和编译一次通过:
I'm not sure it's the only way, but, in order to use the generated class type in a method signature, you can generate the class in 2 steps - though it's still in one file and compiled in one pass:
- 仅使用构造函数调用
gen- class
- 再次使用状态,全套构造函数和方法调用
gen-class
。
- call
gen-class
with only the constructors - call
gen-class
again with state, full set of constructors and methods.
我尝试了各种方法,包括forward声明,但只有上面的工作最终。我没有扩展你的例子一点。注意 copy
方法不是非常有用的,因为 Point
是不可变的,但你可能想要为你的类提供mutator。
I tried with various methods including forward declaration, but only the above was working eventually. I did extend your example a little bit. Note That the copy
method is not very useful as-is since Point
is immutable, but you may want to provide mutators to your class.
完整列表:
(ns points.Point)
;; generate a simple class with the constructors used in the copy method
(gen-class
:name points.Point
:init init
:constructors {[] []
[double double] []})
;; generate the full class
(gen-class
:name points.Point
:prefix pt-
:main true
:state coordinates
:init init
:constructors {[] []
[double double] []}
:methods [[distance [points.Point] double]
[copy [] points.Point]])
(defn pt-init
([] (pt-init 0 0))
([x y]
[[] {:x x :y y}]))
(defn pt-copy
"Return a copy of this point"
[this]
(points.Point. (:x (.coordinates this)) (:y (.coordinates this))))
(defn pt-distance [^points.Point this ^points.Point p]
(let [dx (- (:x (.coordinates this)) (:x (.coordinates p)))
dy (- (:y (.coordinates this)) (:y (.coordinates p)))]
(Math/sqrt (+ (* dx dx) (* dy dy)))))
(defn pt-toString [this]
(str "Point: " (.coordinates this)))
;; Testing Java constructors and method call on Point class
(import (points Point))
(defn pt-main []
(let [o (Point.)
p (points.Point. 3 4)]
(println (.toString o))
(println (.toString p))
(println (.distance o p))
(println (.distance p (.copy p)))))
要生成类,请使用
In order to generate the classes, configure project.clj
with the line
:aot [points.Point]
使用 lein测试
给出:
tgo$ lein clean
tgo$ lein compile
Compiling points.Point
tgo$ lein run
Point: {:x 0, :y 0}
Point: {:x 3.0, :y 4.0}
5.0
0.0
这篇关于clojure gen-class返回自己的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!