Analysis of the implied this reference in the Java subclass and the parent class

Source: Internet
Author: User

/*Take a look at the following procedure to see if your answer is consistent with the running answer! */classparent{ Public intx;  PublicParent p;  PublicParent () {} PublicParent (intx) {            This. x=x; P= This; }          Public voidf () {System.out.println ("Parent::f ()"); }               Public voidg () {System.out.println ("Parent::g ()"); }          Public voidh () {System.out.println ("Parent::h ()");            f ();            g ();            Y (); System.out.println ("Look here:" +x); }             Private voidy () {System.out.println ("Parent::y ()"); }};classChildextendsparent{ Public intx;  PublicChild () {} PublicChild (intx) {             Super(x+5);  This. x=x; }         Public voidf () {System.out.println ("Child F ()"); }         Public voidg () {System.out.println ("Child g ()"); }}; Public classThisdemo { Public Static voidMain (string[] args) {Child ch=NewChild ();            CH.H ();            System.out.println (); CH=NewChild (5);            CH.H ();            System.out.println ();    Ch.p.h (); }} actually C The idea of ++this is similar to this in Java, that is, in polymorphic situations there are some differences, if the method in the C + + base class does not have a virtual modification, then the overridden method of the derived class is not overwritten and does not contain polymorphic (c+ + Polymorphic types: Forced polymorphic, overloaded polymorphic, type-parameterized polymorphic, including polymorphic (by using virtual)! in Java, however, if the subclass overrides the method of the parent class, then overwrite is generated, like C+ + Use the polymorphism of virtual!  C ++ Please click here for sample: here! Output Result:/*parent::h () child F () child g () parent::y () look here:0parent::h () Child F () child g () parent::y () Look here:10// The output is xparent::h in the parent class () child F () child g () parent::y () Look here:10//output is the x in the parent class*/ 

Analysis of the implied this reference in the Java subclass and the parent class

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.