java如何从AsubClass引用类
我试图找到答案,但我找不到。在java中,当我创建一个子类时,如何引用第一级类?使用“this”访问子类,所以我不能。另一个选项是将参数传递给子类,但我想知道是否有最简单的方法
//here there is my other 1st level class implementation, this is a JFrame
btnNewProject.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
VehicleScreen pp=new VehicleScreen(**here should go the top class reference**);
//v is a JPanel that I pass to allJframes
v.setContentPane(pp);
v.setVisible(true);
}
});
# 1 楼答案
# 2 楼答案
您有两个选择:
将匿名类之外的变量设为实例变量:
您还可以访问实例变量,如下所示:
对局部变量使用
final
修饰符:请注意,使用^{} modifier will mean you cannot re-assign the variable at a later point in your program.