这是什么类型的 Java 构造函数调用?
我从来没有遇到过这样的事情,我不知道这种类型的编码! 这是什么? (我对 Java 还很陌生)
DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
// code
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
// code
}
public void characters(char ch[], int start, int length) throws SAXException {
// code
};
在构造函数调用之后有一个大括号(!?),并且似乎覆盖了某些方法。然后大括号以分号结束。我从未在构造函数调用后见过大括号。正常吗?怎么称呼?谢谢你!
ps:在 Eclipse 上,如果我删除分号,它会显示 LocalVariableDeclarationStatement
错误。
I've never encountered something like this and I don't know this type of coding!
What is this? (I'm pretty new to Java)
DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
// code
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
// code
}
public void characters(char ch[], int start, int length) throws SAXException {
// code
};
After constructor calling there is a brace (!?) and it seems that there is an overriding of some methods. Then the brace is terminated with a semicolon. I've never seen brace after a constructor call. Is it normal? How is it called? Thank you!
p.s: on Eclipse, if i remove the semicolon, it says LocalVariableDeclarationStatement
error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个匿名类。
当您想要创建从另一个类或接口派生的类,但不需要在代码中的其他任何地方使用新类时,匿名类会很有用。
在您的具体情况下
DefaultHandler
类是一个辅助类,它实现了多个接口(EntityResolver
、DTDHandler
、ContentHandler
、ErrorHandler< /代码>) 通过提供什么都不做的方法。这个想法是,您可以从此类派生并仅重写您需要的特定方法。这比直接实现接口的代码要少得多,因为这样您将需要为每个方法提供定义,包括您不打算使用的方法。
That's an anonymous class.
Anonymous classes can be useful when you want to create a class that derives from another class or interface but you don't need to use your new class anywhere else in your code.
In your specific case the
DefaultHandler
class is a helper class that implements several interfaces (EntityResolver
,DTDHandler
,ContentHandler
,ErrorHandler
) by providing methods that do nothing. The idea is that you can derive from this class and override only the specific methods you need. This can be much less code than directly implementing the interfaces because then you will need to provide definitions for every method, including methods that you don't intend to use.这是匿名类定义。 DefaultHandler 是一个接口,没有实现,您在创建实例时就在那里创建一个接口。
由于
DefaultHandler
是一个接口,因此它需要一个实现DefaultHandler
接口的类的对象。但是,如果没有这样的类或者您需要一个不同的类,您可以通过动态实现接口来创建一个满足此要求的对象。this is anonymous class definition. DefaultHandler is an interface and has no implementation and you are creating one just there, while creating an instance.
since
DefaultHandler
is an interface it expects an object of class which implementsDefaultHandler
interface. But if there is no such class or you need a different one you can create an object that satisfies this requirement by implementing the interface on the go.这是一个匿名内部类。谷歌搜索“anonymous class java”。它基本上是一个从接口动态创建的类。整个定义是内联指定的,将其视为“new DefaultHandler()”部分之后的类定义,显然这个特定的类定义只能在这个地方使用。
It's an anonymous inner class. Have a google for 'anonymous class java'. It's basically a class that's created on the fly from an interface. The entire definition is specified inline think of it as a class definition after the 'new DefaultHandler()' part obviously this specific class definition can only be used in this place.
这是一个匿名类的定义。它基本上是一种实现抽象类或扩展类的方法。因此,您要么实现
DefaultHandler
类,要么扩展
DefaultHandler
,具体取决于DefaultHandler
是否是>抽象
类或具体类。Thats an Anonymous class definition. It basically is a way to implement an Abstract class or extend a class. So, you are either implementing the
DefaultHandler
class orextend
ing theDefaultHandler
depending on whetherDefaultHandler
is anabstract
class or a concrete class.匿名类使用这个。 http://docstore.mik.ua/orelly/java-ent/jnut /ch03_12.htm
Anonymous class uses this. http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm