gpt4 book ai didi

Java - 从匿名内部类访问变量

转载 作者:行者123 更新时间:2023-11-29 06:00:02 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Cannot refer to a non-final variable inside an inner class defined in a different method

我只是在试验,有一个问题。

当我从匿名内部类访问非最终类变量时,为什么这是可以接受的:

static JLabel e = new JLabel("");
public static void main(String[] args) {

JButton b = new JButton("ok");
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
String l = e.getText();

}

});

}

但如果没有 final 修饰符,以下内容是 Not Acceptable :

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