这是什么类型的 Java 构造函数调用?

发布于 2025-01-03 05:33:07 字数 921 浏览 1 评论 0原文

我从来没有遇到过这样的事情,我不知道这种类型的编码! 这是什么? (我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‖放下 2025-01-10 05:33:07

这是一个匿名类

当您想要创建从另一个类或接口派生的类,但不需要在代码中的其他任何地方使用新类时,匿名类会很有用。

匿名类最优雅的一点是,它们允许您在需要的地方准确地定义一次性类。此外,匿名类具有简洁的语法,可以减少代码中的混乱。

在您的具体情况下 DefaultHandler 类是一个辅助类,它实现了多个接口(EntityResolverDTDHandlerContentHandlerErrorHandler< /代码>) 通过提供什么都不做的方法。这个想法是,您可以从此类派生并仅重写您需要的特定方法。这比直接实现接口的代码要少得多,因为这样您将需要为每个方法提供定义,包括您不打算使用的方法。

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.

One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a succinct syntax that reduces clutter 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.

暗恋未遂 2025-01-10 05:33:07

这是匿名类定义。 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 implements DefaultHandler 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.

一桥轻雨一伞开 2025-01-10 05:33:07

这是一个匿名内部类。谷歌搜索“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.

还给你自由 2025-01-10 05:33:07

这是一个匿名类的定义。它基本上是一种实现抽象类或扩展类的方法。因此,您要么实现 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 or extending the DefaultHandler depending on whether DefaultHandler is an abstract class or a concrete class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文