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

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

问题描述

因此,当研究lambda表达式并使用它们来替换Java中的EventHandlers的匿名内部类时,我遇到了一些让我停下来思考的匿名内部类。例如,当为通常实现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而不是直接实现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的匿名内部类而不是直接来自它,因为我在第二个代码中显示了
snippet?

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.

一个body,它是一个类声明体。更具体地说,在正文中,方法声明是允许的,但语句不是。

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天全站免登陆