gpt4 book ai didi

Java语法: statement in {} after new

转载 作者:行者123 更新时间:2023-12-02 16:16:13 24 4
gpt4 key购买 nike

这是什么意思?

addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
String location = GUI.Custom.QuickDialogs.selectFile(false);
try
{
PrintWriter pw = new PrintWriter(new File(location));
String text = textArea.getText();
pw.println(text);
pw.flush();
pw.close();
}
catch(Exception ex)
{
textArea.append("Could not save this debug output");
}
}
});

new ActionListener() {} {} 中发生了什么?在对象?类中声明一个方法? ActionListener 是内部类吗?

最佳答案

new ActionListener() 后面的大括号内的声明是 anonymous class 的定义。扩展 ActionListener

在您的例子中,匿名类提供了单个方法actionPerformed的实现。当您需要仅在代码中的单个位置使用的类时,此功能可让您减少代码的大小并使声明更接近使用它们的位置。

关于Java语法: statement in {} after new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com