2

我有以下java类。

public class Toto {
    public void aMethod(A a) {
        System.out.println("A " + a);   
    }

    public void aMethod(B b) {
        System.out.println("B " + b);
    }
}

我想覆盖 aMethod(A a) 而不是 aMethod(B b)。我能做到的唯一方法是:

(ns titi
    (:gen-class :extends Toto
     :exposes-method {aMethod parentMethod}))

(defn- -aMethod [this x]
    (if (= (type x) A)
        (println "ok do something here")
        (.parentMethod this x)))

有一个更好的方法吗?(我的意思是自己不检查 x 的类型)。

4

1 回答 1

1

根据http://dishevelled.net/Tricky-uses-of-Clojure-gen-class-and-AOT-compilation.html:gen-class可以通过签名覆盖,但我找不到相关文档,所以我猜测它可以被认为是“内部”,它可能会在未来打破。

无论如何,假设你有你的Totojpkg和两个空类AB,这对我有用:

(ns Titi
(:import [jpkg A])
    (:gen-class :extends jpkg.Toto
                :exposes-method [aMethod]))

(defn- -aMethod-A [this x]
    (println "clj a"))

如以下main命名空间:

(ns main
    (:import [jpkg A B]))

(set! *warn-on-reflection* true)

(defn -main []
    (. (new Titi) aMethod (new A))
    (. (new Titi) aMethod (new B)))

印刷:

clj a
B jpkg.B@6631f5ca

代替:

clj a
clj a

输入提示似乎在这里没有帮助。

于 2014-09-01T16:49:27.917 回答