How to invoke superclass' method in a Clojure gen-class method?

亡梦爱人 提交于 2019-12-23 07:25:22

问题


I'm trying to create a class that extends input stream Clojure via gen-class. If I want to invoke the parent class' method, how do I do that?


回答1:


From (doc gen-class)1:

:exposes-methods {super-method-name exposed-name, ...}

It is sometimes necessary to call the superclass' implementation of an
overridden method.  Those methods may be exposed and referred in 
the new method implementation by a local name.

So, in order to be able to call the parent's fooBar method, you'd say

(ns my.custom.Foo
  (:gen-class
    ; ...
    :exposes-methods {fooBar parentFooBar}
    ; ...
    ))

Then to implement fooBar:

(defn -fooBar [this]
  (combine-appropriately (.parentFooBar this)
                         other-stuff))

1 In addition to the :gen-class facility provided by ns forms, there is a gen-class macro.




回答2:


This is not an answer to your actual question, but I have a little library to let you pretend InputStream is an interface instead of a class (so that you don't need gen-class at all). Check out io.core.InputStream, which lets you reify io.core.InputStreamable and get out a customized InputStream. Whatever instance fields you need can just be locals closed over by the reify.



来源:https://stackoverflow.com/questions/9060127/how-to-invoke-superclass-method-in-a-clojure-gen-class-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!