简体   繁体   English

如何使用自定义构造函数实现ActionListener?

[英]How do I implement an ActionListener with custom constructor?

I need to implement an action listener with a custom constructor so that I can pass parameter to it. 我需要使用自定义构造函数实现动作侦听器,以便可以将参数传递给它。

     class CustomActionListener implements ActionListener{

        @Override
        public ActionListener(int u){

        }



        @Override
        public void actionPerformed(ActionEvent arg0) {

        }
    }

but It seems I can't override constructors.How do I do it? 但似乎我无法覆盖构造函数。我该怎么做?

ActionListener is an interface, there is no constructor in it. ActionListener是一个接口,其中没有构造函数。

More over you can not override constructors. 另外,您不能覆盖构造函数。 In extended class constructor, you need to call super constructor if no default constructor is there in super class . 在扩展类构造函数中,如果super class中没有默认构造函数,则需要调用super构造函数。

You just need to call super class constructor before anything else. 您只需要先调用超类构造函数即可。 Sounds simple to me if that's what you mean: 如果那是您的意思,对我来说听起来很简单:

public class CustomActionListener implements ActionListener{

    private int u;

    public CustomActionListener(int u) {
        super();
        this.u = u;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM