我现在正在使用 Clojure 创建一个类对象,它有一个返回对象本身的方法。
用Java编写的,我想要制作的对象是这样的,
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 代码如下:
(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
虽然我用谷歌搜索了这个问题,但找不到任何答案。有谁知道这个问题的解决方案?
预先感谢您的帮助。
我不确定这是唯一的方法,但是,为了在方法签名中使用生成的类类型,您可以分两步生成该类 - 尽管它仍然在一个文件中并一次性编译:
- call
gen-class
仅使用构造函数
- call
gen-class
再次使用状态、全套构造函数和方法。
我尝试了各种方法,包括前向声明,但最终只有上述方法有效。我确实稍微扩展了你的例子。请注意,copy
方法不是很有用as-is since Point
是不可变的,但您可能想为您的类提供修改器。
完整清单:
(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)))))
为了生成类,配置project.clj
与线
:aot [points.Point]
测试用lein
gives:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)