简体   繁体   中英

Clojure : call a java subclass of abstract class

I have a problem today because I do not know how to call a java class which extends a sublass. I know there is the $ sign for nested class but here I'm lost.

To be more clear, here are the classes :

Abstract class :

http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/fitting/AbstractCurveFitter.html

Subclass :

http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/fitting/SimpleCurveFitter.html

To be honest, I forgot a lot of thing about java but I see there is no constructor for the subclass. The problem is that I must use the sublass.

Here is the code

(defn apache-logistic-reg [data x0 lamb]
  (let [f (Logistic$Parametric.)
        start (def-start x0 lamb)
        fitter (.create SimpleCurveFitter. f start)
        points (double-array (extract-points data))]
    (.fit fitter points)))

The problem is that I cannot call SimpleCurveFitter. because it has no constructor. And AbstractCurveFitter has no .create, plus if I remember it well abstract classes cannot be cast.

And if I remember well, the constructor of SimpleCurveFitter would have the name of its abstract class but maybe I'm wrong.

How can I do ?

Thanks you

You don't need to call a constructor in SimpleCurveFitter , because it provides the static method create to get an instance for you (which you're already trying to use). Just make a static method call to that method to get an instance:

(.create SimpleCurveFitter  f start) ;; No '.' after SimpleCurveFitter
(. SimpleCurveFitter create f start) ;; Alternate syntax
(SimpleCurveFitter/create   f start) ;; Most common syntax (pointed out by amalloy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM