Essay a call between classes in which Java has an inheritance relationship

Source: Internet
Author: User

1 /**2 * Created by ZMT on 2016/12/22.3  */4  Public classTest {5      Public Static voidmain (String [] args) {6System.out.println (NewB (). GetValue ());7     }8     Static classa{9         protected intvalue;Ten          PublicAintv) { One SetValue (v); A         } -          Public voidSetValue (intvalue) { -              This. Value =value; the         } -          Public intGetValue () { -             Try{ -value++; +                 returnvalue; -}Catch(Exception e) { + System.out.println (e.tostring ()); A}finally { at                  This. SetValue (value); - System.out.println (value); -             } -             returnvalue; -         } -     } in     Static classBextendsa{ -          PublicB () { to             Super(5); +SetValue (GetValue ()-3); -         } the          Public voidSetValue (intvalue) { *             Super. SetValue (2 *value); $         }Panax Notoginseng     } -}

Debug to see the execution process.

The first is the main method, new a B object, and then the GetValue () method that invokes the object, which is nothing to say.

And then we're going to do the B-class construction, and there's nothing to say.

Then execute the construction method of B, Super (5), that is, the constructor of the parent class A called B, should be to the SetValue () method of the A construct, and the value of the member variable value of a is assigned to 5, which can be clearly seen through debugging.

The next step is to execute the SetValue () method, but at this point both Class A and Class B have a SetValue () method, which I initially thought was a Class A SetValue () method, but the result is not, look at the debugging process.

Executes the SetValue () method of B, because the constructor of Class B is now being executed , so the method in class B is called by default, and the method in its parent class A is called if it is not in class B. We continue to see that, next to Super.setvalue (2 * value), which is to execute the Class A SetValue () method, the member variable value of Class A should be changed to 10


Continue looking down, the Super (5) in the constructor of Class B is done, and then it's SetValue (GetValue ()-3) method

Then execute the GetValue () method, first found in Class B, but Class B does not have a GetValue () method, so the Class A GetValue () method is executed, Class A must be there, otherwise the compilation will not pass

Then start executing a try, catch, finally this piece, give the member of a variable value self-increment, from the previous 10 to 11, and then directly return value, no catch exception, continue to finally inside the This.setvalue (value)

And then whether this refers to the Class A or class B, the answer is class B, because now is the constructor of B, so this refers to Class B , which is called the SetValue (int value) method of Class B.

Then Super.setvalue (2 * value), executes the setValue (int value) of the parent Class A, passes 2 * 11 as a parameter, and the Class A setValue (int value) assigns the value passed in to the member variable of the value , became 22.

Then This.setvalue (value) is finished, and the final output value,22

Here the GetValue () method executes, but one thing to note is that the value at this time is 22, but the return value of GetValue () is 11, because it is returned in try{}, so the return value level of this method has been saved, is 11, The return value of GetValue () will not change, even though finally{} has changed the value. Then proceed to SetValue (GetValue ()-3) in Class B construction method, GetValue () is 11, so the parameter of B's SetValue (int value) method is 8, then super.setvalue (2 * value)

Call the SetValue (int value) method of Class A, while assigning the parameter to the member variable value of Class A, at which point value becomes 16

The constructor for Class B is all done, that is, new B (), and then the GetValue () method of the object is called, Class B is not, but the parent class A has, so,

Continue try{}, catch{}, finally{the member variable of the},a class value is 16, then value++, and then return, then the return value of GetValue () has been determined, is 17, even if the value is changed in Finally, Its return value does not change. Then to finally{}, again This.setvalue (value), as mentioned earlier, this refers to the Class B's this, so call Class B's SetValue (int value)

Then again Super.setvalue (2 * value), call the Class A of SetValue (), and pass 2 * 17 as a parameter passed past.

Assigns a parameter to the member variable value of a, at which time This.setvalue (value) is executed, and value is 34. Finally output value.

It should be noted that the return value of the GetValue () method At this point is 17, as mentioned earlier, where the entire new B (). GetValue () is executed, and finally the return value of the getValue is output, which is 17. so the output of the whole process is 22, 34, 17 ...


Although this problem around a lot of bends, but we do not realize that the overall process is not very complex, that is, the class inherits from the parent class, calling the method first call the method in the subclass, if there is no call to the method in the parent class, there is a point is try{}, catch{}, finally{} return value of the problem, Once a value is returned in try{}, if finally has a return value,the return value in finally overrides the return value of the try, or the return value in try if finally has no return value. With this in hand, the problem is very simple.

Essay a call between classes in which Java has an inheritance relationship

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.