I recently read some excellent open-source project source code using Eclipse, and found a trap that beginners can easily access. The description is as follows:
Assume there are two classes. The Sub class inherits from the Base class of the parent class. The Code is as follows:
// Base class
Public class Base {
Public void say (){
SayHello ();
}
Public void sayHello (){
System. out. println ("Base say Hello! ");
}
}
// Sub class
Public class Sub extends Base {
@ Override
Public void sayHello (){
System. out. println ("Sub say Hello! ");
}
Public void test (){
Super. say ();
}
}
// Test class TestCase
Public class TestCase {
Public static void main (String [] args ){
Sub sub = new Sub ();
Sub. test (); // The result is "Sub say Hello! "Or" Base say Hello! "?
}
}
If you see that the result is "Sub say Hello! ", Congratulations! Your basic Java skills are solid. Call super in the test () method of the Sub class. say (), can be simply understood as a common function call, the super. after the say () call is replaced with the implementation code, the Sub implementation code is as follows:
Public class Sub extends Base {
@ Override
Public void sayHello (){
System. out. println ("Sub say Hello! ");
}
Public void test (){
SayHello (); // implementation code of the super. say () call
}
}
At this point, the result should be obvious. Here sayHello () points to Sub. sayHello () method, so the output result is "Sub say Hello! ".
After finishing the background, let's take a look at the code tracking function in Eclipse:
Press Ctrl and click super. say (). The Jump Result in Eclipse is as follows:
Press Ctrl and click sayHello () in line 1. The jump result is as follows:
Seeing such jump results will make many beginners confused. We hope that this function can be corrected as soon as possible in the future Eclipse version. After all, this is very easy for the Eclipse development team.
This article is from the "Chunhua qiushi" blog