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.
我一直在阅读Programming Clojure,我在理解Stuarts主Java Interop示例时遇到了一些麻烦。他扩展了DefaultHandler,并创建了一个startElement方法,然后将该处理程序传递给XML解析器。我不明白的是,到底发生了什么。他的startElement实现是否覆盖了DefaultHandler中定义的实现?我糊涂了。我没有使用Java的经验,也没有面向对象的经验。
Thanks!
2 个解决方案
#1
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:
你说它的作用是正确的。 proxy语句创建一个新类,即此Java代码的等效项:
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.
因此,proxy语句定义了该类,并为您提供了一个实例,该实例现在保存在print-element-handler中。
#2
Glancing over the Java documentation for DefaultHandler answered my own question. http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29
浏览DefaultHandler的Java文档回答了我自己的问题。 http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java。 lang.String,%20org.xml.sax.Attributes 29%
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
默认情况下,什么也不做。应用程序编写者可以在子类中重写此方法,以在每个元素的开头采取特定操作(例如,分配新的树节点或将输出写入文件)。
#1
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:
你说它的作用是正确的。 proxy语句创建一个新类,即此Java代码的等效项:
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.
因此,proxy语句定义了该类,并为您提供了一个实例,该实例现在保存在print-element-handler中。
#2
Glancing over the Java documentation for DefaultHandler answered my own question. http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29
浏览DefaultHandler的Java文档回答了我自己的问题。 http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java。 lang.String,%20org.xml.sax.Attributes 29%
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
默认情况下,什么也不做。应用程序编写者可以在子类中重写此方法,以在每个元素的开头采取特定操作(例如,分配新的树节点或将输出写入文件)。