java 必须实现继承的抽象方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17128126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:04:34  来源:igfitidea点击:

Must implement the inherited abstract method

javaswinginterfacenested-classactionevent

提问by Daniel

My class implements ActionListener. I have implemented the following nested classes below:

我的班级实现了 ActionListener。我在下面实现了以下嵌套类:

JMenuItem mntmNew = new JMenuItem("New...");
    mntmNew.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            doNew(e); //calls to outer class for cleaner code
        }
    });
    mnFile.add(mntmNew);

    JMenuItem mntmLoad = new JMenuItem("Load...");
    mntmLoad.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            doLoad(e); //calls to outer class for cleaner code
        }
    });
    mnFile.add(mntmLoad);

//etc. for the rest of the menu system

However, Eclipse is still telling me that my class must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent e). Can you not implement override methods in a nested class in this way?

但是,Eclipse 仍然告诉我我的类必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent e)。不能以这种方式在嵌套类中实现覆盖方法吗?

回答by Hovercraft Full Of Eels

Your question:

你的问题:

Can you not implement override methods in a nested class in this way?

不能以这种方式在嵌套类中实现覆盖方法吗?

The answer is no. Eclipse (actually Java) is complaining that while you're declaring your class as implementing ActionListener you're not giving your class the necessary actionPerformed(...)method in the class's own scope -- and this last part is veryimportant. The class that implements the interface must implement all the interface's required methods in its own scope and not in nested classes. Note that this doesn't prevent you from nesting classes that also implement ActionListener or other interfaces, but regardless, the rule remains that a non-abstract class that implements an interface must override allof the interface's methods.

答案是否定的。Eclipse(实际上是Java)抱怨说,当您将类声明为实现 ActionListener 时,您并没有actionPerformed(...)在类自己的范围内为您的类提供必要的方法——这最后一部分非常重要。实现接口的类必须在其自己的范围内实现接口所需的所有方法,而不是在嵌套类中。请注意,这不会阻止您嵌套也实现 ActionListener 或其他接口的类,但无论如何,规则仍然是实现接口的非抽象类必须覆盖该接口的所有方法。

But since you're not using objects of your class as an ActionListener, the simple solution is to not declare your class as implementing the ActionListener interface. Problem solved. And actually you're far better off not having your GUI class implement your listener interfaces since combining them in one class is asking a class to do too much. In technical terms, it unnecessarily reduces a class's cohesion and risks increasing it's coupling reducing its readability and maintainability.

但是,由于您没有将类的对象用作 ActionListener,因此简单的解决方案是不要将类声明为实现 ActionListener 接口。问题解决了。实际上,最好不要让 GUI 类实现侦听器接口,因为将它们组合在一个类中会要求类做太多事情。从技术角度讲,它不必要地降低了类的内聚力,并有可能增加其耦合度,从而降低其可读性和可维护性。