A small problem with Java

Source: Internet
Author: User

The teacher threw a question in the discussion group today and asked us to try to think about what the code output was.

The code is as follows:

  

classT {voidFoo () { This. Bar ();} voidBar () {System.out.println ("T.bar"); }     Public Static voidMain (String args[]) {b b=NewB ();    B.foo (); }}classBextendsT {voidFoo () {Super. Foo ();} voidBar () {System.out.println ("B.bar"); }}

Think of a wave:

Instantiating subclass objects, the subclass parent does not have an explicit constructor, which is the way to jump between child class parent methods.

"Well, the answer must be ' T.bar '".

But still knocked over, found that the answer is ' b.bar ' ... ()

The roommate said: "Yes, this is not pointing to the current object ..."

(PA, slap, face ...) Pain ...)

Re-examined again, only to find that I still do not understand the place ...

In other words, the entire code execution process

The problem is the point of This.bar () ... Usually encountered this generally will be in this class (here is T Class) to find out whether there will be the method of implementation, if there is, it is.

  But this time the difference, in fact, is this, so that the current method points to its specific object, when this object implements this method (the bar () method is covered in Class B here), natural This.bar () will point to the subclass implementation of the bar () method.

In essence, the output B.bar is a problem covered by the method, but the cause of its own error is the point of this problem.

So, when we change the code a little bit.

The bar method of Class B is commented out so that it cannot overwrite the bar method of its parent class T, and this is still a pointer to Class B object, but when invoking the bar method, it is found that Class B does not have the implementation of the Bar method, so "trace" to the parent class of B to find the Bar method, At this point the bar method of the parent class is found and called, and the output is naturally t.bar ().

  

At this point, let's look at another change: just delete this in the parent class T, run the result

As you can see, the output of the program is still b.bar. (It's more or less than I expected)

That is, the program at this time, and contains the This keyword, is the same ...

This question to my impression is still very deep, after all, after reading the question, unexpectedly a bit not modestly directly shouted out a wrong answer ...

Will be a lot of attention in the future of this and super so easy to "change" the program flow of keywords ...

A small problem with Java

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.