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

发布于 2024-09-05 13:12:51 字数 412 浏览 11 评论 0原文

我将如何为实现接口的内部类编写构造函数?我知道我可以创建一个全新的类,但我认为必须有一种方法可以按照以下方式执行某些操作:

JButton b = new JButton(new AbstractAction() {

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


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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄断弥空 2024-09-12 13:12:51

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

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

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

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

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

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");
    }
}); 
遗弃M 2024-09-12 13:12:51

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

JButton b = new JButton(new AbstractAction() {

    {
        // Do whatever initialisation you want here.
    }

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

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

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

private class MyAction extends AbstractAction {

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

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

然后:

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

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.

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());
为你鎻心 2024-09-12 13:12:51

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

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

The resulting class is not of type AbstractAction but 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.

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

你在看孤独的风景 2024-09-12 13:12:51

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文