【问题标题】:how to call an interface method in the same class?如何在同一个类中调用接口方法?
【发布时间】:2021-04-08 06:31:57
【问题描述】:

我在一个类中创建了一个接口。我想在同一个类中调用这个接口的一个方法,只是传递参数给它

public class NewArticle extends JFrame {
    interface NewArticleEvent{
        public void addarticle(String name ,String category,String B);        
    }
   .
   .
   .
    JButton add= new JButton("add");
    add.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            /* I want to call 'addarticle' method here * /

        }
    });
   .
   .
   .
}

我想在这里使用我定义的 3 个参数调用我的方法 addarticle

  • l1.gettext().tostring()
  • l2.gettext().tostring()
  • l3.gettext().tostring()

因为我想在另一个类中将此方法与这些参数一起使用。

【问题讨论】:

  • 如果你有一个实现 NewArticleEvent 的实例,你可以调用 instance.addarticle()。
  • 也许您可以在接口NewArticleEvent 中创建方法addarticle default method

标签: java class interface actionlistener


【解决方案1】:

您需要传递一个 NewArticleEvent 的实例才能使用它的方法。

解决方案可能是将实现 NewArticleEvent 的对象传递给 NewArticle 类,并在需要的地方调用方法。进一步的代码将不胜感激。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多