我只是好奇。有没有办法访问另一个匿名类中的匿名类中的父类?
我让这个例子创建一个 JTable
子类(匿名类)覆盖 changeSelection
并在里面我创建另一个匿名类。
MCVE:
public class Test{
public static void main(String args []){
JTable table = new JTable(){
@Override
public void changeSelection(
final int row, final int column,
final boolean toggle, final boolean extend) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
super.changeSelection(row, column, toggle, extend);
//more code here
}
});
}
};
}//end main
}//end test
如何引用 super.changeSelection(..)
?
最佳答案
不幸的是,你必须给外部匿名类起一个名字:
public class Test{
public static void main(String args []){
class Foo extends JTable {
@Override
public void changeSelection(
final int row, final int column,
final boolean toggle, final boolean extend) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Foo.super.changeSelection(row, column, toggle, extend);
//more code here
}
});
}
};
JTable table = new Foo();
}//end main
}//end test
关于java - 从内部匿名类访问外部匿名类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18062961/