相关疑难解决方法(0)

Java执行流程 - 重写方法首先执行,而不是构造函数

我在同一个java文件中有以下代码.

import javax.swing.SwingUtilities;
import java.io.File;

public class MainClass2{
   public static void main(String[] args){
       SwingUtilities.invokeLater(new Runnable(){
             public void run() {
                 javax.swing.JFileChooser jfc = new MyFileChooser();
                     File file = jfc.getSelectedFile();
             }

      });
   }
}

class MyFileChooser extends javax.swing.JFileChooser{
    public MyFileChooser(){
        System.out.println("constructor call");
    }
    @Override
    public java.io.File getSelectedFile(){
        System.out.println("call to getSelectedFile");
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,输出给了我

call to getSelectedFile

constructor call

call to getSelectedFile

输出不应该是

constructor call

call to getSelectedFile

我正在使用java 5.

java debugging

10
推荐指数
1
解决办法
980
查看次数

标签 统计

debugging ×1

java ×1