java 内部类中的构造函数(实现接口)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3045130/
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-10-30 00:05:18  来源:igfitidea点击:

Constructors in Inner classes (implementing Interfaces)

javaconstructorinner-classesanonymous-inner-class

提问by thepandaatemyface

How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this:

我将如何为实现接口的内部类编写构造函数?我知道我可以创建一个全新的类,但我认为必须有一种方法可以做到这一点:

JButton b = new JButton(new AbstractAction() {

    public AbstractAction() {
        super("This is a button");                        
    }


    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler asks for return type). Does anyone have an idea?

当我输入它时,它不会将 AbstractAction 方法识别为构造函数(编译器要求返回类型)。有没有人有想法?

回答by Itay Maman

Just insert the parameters after the name of the extended class:

只需在扩展类的名称后插入参数:

JButton b = new JButton(new AbstractAction("This is a button") {

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

Also, you can use an initialization block:

此外,您可以使用初始化块:

JButton b = new JButton(new AbstractAction() {

    {
       // Write initialization code here (as if it is inside a no-arg constructor)
       setLabel("This is a button")
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

回答by DaveJohnston

If you really need a contructor for whatever reason, then you can use an initialization block:

如果出于某种原因确实需要构造函数,则可以使用初始化块:

JButton b = new JButton(new AbstractAction() {

    {
        // Do whatever initialisation you want here.
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}); 

But you can't call a super-class constructor from there. As Itay said though, you can just pass the argument you want into the call to new.

但是您不能从那里调用超类构造函数。正如 Itay 所说,你可以将你想要的参数传递给 new 的调用。

Personally though, I would create a new inner class for this:

不过就我个人而言,我会为此创建一个新的内部类:

private class MyAction extends AbstractAction {

    public MyAction() {
        super("This is a button.");
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked");
    }
}

then:

然后:

JButton b = new JButton(new MyAction());

回答by Andreas Dolk

The resulting class is not of type AbstractActionbut of some (unnamed, anonymous) type that extends/implements AbstractAction. Therefore a constructor for this anonymous class would need to have this 'unknown' name, but not AbstractAction.

生成的类不是类型,AbstractAction而是某些(未命名的、匿名的)扩展/实现的类型AbstractAction。因此,此匿名类的构造函数需要具有此“未知”名称,但不需要AbstractAction.

It's like normal extension/implementation: if you define a class House extends Buildingand construct a Houseyou name the constructor Houseand not Building(or AbstractActionjust to com back to the original question).

这就像正常的扩展/实现:如果您定义 aclass House extends Building并构造 aHouse您命名构造函数House而不是Building(或AbstractAction只是为了回到原始问题)。

回答by programmar

The reason the compiler is complaining is because you are trying to declare a constructor inside your anonymous class, which is not allowed for anonymous classes to have. Like others have said, you can either solve this by using an instance initializer or by converting it to a non-anonymous class, so you can write a constructor for it.

编译器抱怨的原因是你试图在匿名类中声明一个构造函数,这是匿名类不允许的。就像其他人所说的那样,您可以通过使用实例初始值设定项或将其转换为非匿名类来解决此问题,因此您可以为其编写构造函数。