Anonymous inner class using an interface in Java

旧巷老猫 提交于 2019-11-28 12:04:14

问题


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
    }
});

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());

So my questions are:

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) 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) 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?

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.


I copy this from 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.

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

  • 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.

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.)



来源:https://stackoverflow.com/questions/26581639/anonymous-inner-class-using-an-interface-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!