Nat*_*n H 2 java binding overloading method-dispatch
我正在练习考试,发现了一个让我完全迷失的样本问题.对于以下代码,找到输出的内容:
class Moe {
public void print(Moe p) {
System.out.println("Moe 1\n");
}
}
class Larry extends Moe {
public void print(Moe p) {
System.out.println("Larry 1\n");
}
public void print(Larry l) {
System.out.println("Larry 2\n");
}
}
class Curly extends Larry {
public void print(Moe p) {
System.out.println("Curly 1\n");
}
public void print(Larry l) {
System.out.println("Curly 2\n");
}
public void print(Curly b) {
System.out.println("Curly 3\n");
}
}
public class Overloading_Final_Exam {
public static void main (String [] args) {
Larry stooge1 = new Curly();
Moe stooge2 = new Larry();
Moe stooge3 = new Curly();
Curly stooge4 = new Curly();
Larry stooge5 = new Larry();
stooge1.print(new Moe());
((Curly)stooge1).print(new Larry());
((Larry)stooge2).print(new Moe());
stooge2.print(new Curly());
stooge3.print(new Curly());
stooge3.print(new Moe());
stooge3.print(new Larry());
((Curly)stooge3).print(new Larry());
((Curly)stooge3).print(new Curly());
stooge4.print(new Curly());
stooge4.print(new Moe());
stooge4.print(new Larry());
stooge5.print(new Curly());
stooge5.print(new Larry());
stooge5.print(new Moe());
}
}
Run Code Online (Sandbox Code Playgroud)
我记住了我的想法,但是当我运行java时,我得到了完全不同的东西:
Curly 1 Curly 2 Larry 1 Larry 1 Curly 1 Curly 1 Curly 1 Curly 2 Curly 3 Curly 3 Curly 1 Curly 2 Larry 2 Larry 2 Larry 1
前几个是好的,但后来我真的不明白.有人对这个问题有很好的解释吗?
谢谢
我会先画一张照片......
Moe - print(Moe)
|
Larry - print(Moe), print(Larry)
|
Curly - print(Moe), print(Larry), print(Curly)
Run Code Online (Sandbox Code Playgroud)
然后我会跟踪变量:
Larry - stooge5 - > Larry
stooge1.print(new Moe())
((Curly)stooge1).print(new Larry());
((Larry)stooge2).print(new Moe());
stooge2.print(new Curly());
好吧,这是一个有点棘手的地方(对不起,我在这之前停了一个)
等等...
让我知道如果一直跟着这一切并不适合你.
(更新以澄清下一个)
所以一般规则是:
所以当你有:
Moe stooge2 = new Larry();
stooge2.print(new Moe());
Run Code Online (Sandbox Code Playgroud)
编译器说:
运行时说:
一旦你完成了所有这些,尝试摆脱一些方法,看看它是如何改变的.
| 归档时间: |
|
| 查看次数: |
1638 次 |
| 最近记录: |