A basic Java knowledge that comes to mind from the Eclipse source code jump function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.