Dark Horse programmer--this Keywords

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

First look at a small program:

1 class Person2 {  3     PrivateString name; 4     Private intAge ; 5Person (intAge//when the local variable is age, the member variable is also6     {  7          This. Age = Age;//This can be very well differentiated8     }  9 Person (String name)Ten     {   One          This. name = name;//This is used here to represent the object that called the constructor method A     }   -Person (String name,intAge ) -     {   the          This. Name =name;  -          This. Age =Age ;  -     }   -    +      Public voidspeak () -     {   +System.out.println ("name=" +name+ "... age=" +Age );  A Show ();  at     }   -      Public voidShow () -     {   -System.out.println ( This. Name);  -     }   -}

The above procedure synthesizes the constructor and the use of this. It looks like a case where the local variable has the same name as the member variable.

This represents the reference to the object to which the function belongs.

Simply put: Which object is the object that represents the function that this is located in.

The application of this:

When defining a function in a class, the function is used internally to represent the object when the function is called.

Whenever this type of function is used within this class of objects, it is represented by this.

This statement:

Used to call each other between constructors. such as: this (name);

The THI statement can only be defined in the first row of the constructor. Because initialization is performed first.

A summary of this:

This is used in two ways: 1, to distinguish between variables of the same name, said member and local name of the time, 2, for the constructor between the call.

Note: The general function cannot call the constructor directly, because the this statement cannot be used in a generic function and can only be used between constructors.

Dark Horse programmer--this Keywords

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.