对于以下Java代码段,“run”的方法发生四次。我对这四次“跑步”事件的关系颇感混淆。原始代码很长,我只保留与我的问题相关的部分。关于方法覆盖
1. public final class Job extends AbstractJob {
2. private Job() {
3. }
4. public static void main(String[] args) throws Exception {
5. new Job().run(new Path("testdata"), output, 10);
6. }
7.
8. @Override
9. public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
10. run(input, output, alpha0);
11. return 0;
12. }
13. public void run(Path input, Path output, double alpha0)
14. throws IOException, ClassNotFoundException, InterruptedException {
15. ClusterDriver.run(directoryInput, output, alpha0);
16. }
17. }
我可以低估这段代码的调用顺序,如下所示。
起初,他在第5行的方法被调用。由于其特定的参数设置,3个参数,编译器自动使用第13行中定义的run方法。(如果我们只有第5行中有一个参数,那么编译器将使用第9行中定义的run方法代替
在第9行中定义的run方法,它会调用在第10行,基本上是在13行
定义的run方法run方法是我的理解是否正确?
这是关于方法*重载*,而不是*覆盖*。是的,你的理解是正确的。 – mre
你似乎并不困惑,听起来对我来说很合适。也许你缺乏信心。 ;) –