When you extend a Java class to Clojure and define a method with the same name as that clas…

I’ve been reading through Programming Clojure, and I’ve been having some trouble understanding Stuarts primary Java Interop example. He extends DefaultHandler, and creates a startElement method, and then passes that handler to an XML parser. What I don’t understand, is what exactly is happening. Does his implementation of startElement override the one defined in DefaultHandler? I’m confused. I have no experience with Java, and little with object orientation.

Thanks!

I don’t own the book, but I found the code
and it looks like you’re right. Here is the function (for others to see):

(def print-element-handler
  (proxy [DefaultHandler] []
   (startElement
     [uri local qname atts]
     (println (format "Saw element: %s" qname)))))

You’re right about what it does. The proxy statement makes a new class, the equivilent of this Java code:

public class SomeNewClass extends DefaultHandler {
    public void startElement(String uri,
                     String localName,
                     String qName,
                     Attributes attributes) {
        System.out.println(*stuff*);
    }
}

So the proxy statement defines that class, and gives you an instance, which is now held in print-element-handler.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » When you extend a Java class to Clojure and define a method with the same name as that clas…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录