在 Java 中使用接口的匿名内部类 [英] Anonymous inner class using an interface in Java

查看:22
本文介绍了在 Java 中使用接口的匿名内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在研究 lambda 表达式并使用它们替换 Java 中的 EventHandler 的匿名内部类时,我遇到了一些让我停下来思考的匿名内部类.例如,当为通常实现 ActionListener 的东西编写匿名内部类时,我们写

So, when looking into lambda expressions and using them to substitute anonymous inner classes for EventHandlers in Java, I came across a few anonymous inner classes that made me stop and think. For instance, when writing an anonymous inner class for something that normally implements ActionListener we write

myJButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        //DO SOMETHING
    }
});

我对此的困惑是,ActionListener 是一个接口,所以我认为有必要做一些类似的事情......

My confusion with this is, ActionListener is an interface so I thought it'd be necessary to do something like...

myJButton.addActionListener(new myButtonListener implements ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        //DO SOMETHING
    }
});

但这甚至无法编译.我想这显然是因为如果我们使用私有内部类,我们使用

But this doesn't even compile. I guess the reason I though this is obviously if instead we use a private inner class, we use

private MyButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //DO SOMETHING
    }
}
myJButton.addActionListener(new MyButtonListener());

所以我的问题是:

1) 为什么我们可以直接从接口创建匿名内部类,而不必通过实现接口的类来创建?

1) Why are we able to create an anonymous inner class directly from an interface rather than having to create one through a class that implements the interface?

2) 为什么我无法创建实现 ActionListener 的匿名内部类,而不是直接从它创建,如我在第二个代码片段中所示?

2) Why am I unable to create an anonymous inner class that implements ActionListener instead of directly from it as I show in my second code snippet?

推荐答案

1) 为什么我们可以直接从一个匿名内部类创建一个匿名内部类?接口,而不必通过一个类来创建一个实现接口?

1) Why are we able to create an anonymous inner class directly from an interface rather than having to create one through a class that implements the interface?

2) 为什么我无法创建一个匿名内部类来实现ActionListener 而不是直接来自它,正如我在我的第二个代码中所示片段?

2) Why am I unable to create an anonymous inner class that implements ActionListener instead of directly from it as I show in my second code snippet?

当你使用 implements XXXX 创建一个类时,你是在定义一个类(内部或非内部),你必须给它一个名字,确保我们可以做到这一点,这是我们经常做的事情.而匿名内部类没有名字,更像是一个表达式.

When you create a class using implements XXXX, you are defining a class(inner or non-inner), and you will have to give it a name, sure we can do that and this is what we often do . While anonymous inner class dose not have a name, and it is more like an expression.

我从 http://docs.oracle.com/复制了这个javase/tutorial/java/javaOO/anonymousclasses.html

我认为这会帮助你理解匿名类是什么.

And I think this will help you to understand what anonymous class is.

匿名类是一个表达式.它们就像本地类,只是它们没有名称

An anonymous class is an expression. They are like local classes except that they do not have a name

.匿名类表达式的语法类似于构造函数的调用,只是在代码块中包含了一个类定义.

. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

考虑 frenchGreeting 对象的实例化:

Consider the instantiation of the frenchGreeting object:

    HelloWorld frenchGreeting = new HelloWorld() {
        String name = "tout le monde";
        public void greet() {
            greetSomeone("tout le monde");
        }
        public void greetSomeone(String someone) {
            name = someone;
            System.out.println("Salut " + name);
        }
    };

匿名类表达式包含以下内容:

The anonymous class expression consists of the following:

  • 新运算符

  • The new operator

要实现的接口或要扩展的类的名称.在这个例子中,匿名类正在实现接口 HelloWorld.

The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.

包含构造函数参数的括号,就像普通的类实例创建表达式一样.注意:当你实现一个接口时,没有构造函数,所以你使用一对空括号,如本例所示.

Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.

一个主体,即类声明主体.更具体地说,在正文中,允许使用方法声明,但不允许使用语句.

A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

因为匿名类定义是一个表达式,所以它必须是语句的一部分.在此示例中,匿名类表达式是实例化 frenchGreeting 对象的语句的一部分.(这就解释了为什么右大括号后面有一个分号.)

Because an anonymous class definition is an expression, it must be part of a statement. In this example, the anonymous class expression is part of the statement that instantiates the frenchGreeting object. (This explains why there is a semicolon after the closing brace.)

这篇关于在 Java 中使用接口的匿名内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆