Use of Java this (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/28/2793257.html

Preface

In an instance method or constructor, This references the object pointing to the current object-method call or constructor call. You can use this to reference any member in the instantiation method or constructor.

 

Use this in the field

The most common reason for using this keyword is that the field is hidden by parameters of the method or constructor.

For example, the point class is written as follows:

Public ClassPoint {Public IntX = 0;Public IntY = 0;//ConstructorPublicPoint (IntA,IntB) {x=A; y=B ;}}

However, it can also be written as follows:

Public ClassPoint {Public IntX = 0;Public IntY = 0;//ConstructorPublicPoint (IntX,IntY ){This. X =X;This. Y =Y ;}}

Each parameter of the constructor hides the object field. In the constructor, X is the local copy of the first parameter of the constructor.Point field X. The constructor must use this. X.

 

Use this in the constructor

In the constructor, you can use the this keyword to call another constructor of the class. This is an explicit constructor call. Here isRectangle class:

 Public   Class  Rectangle {  Private   Int  X, Y;  Private   Int  Width, height;  Public  Rectangle (){  This (0, 0, 0, 0);}  Public Rectangle ( Int Width, Int  Height ){  This (0, 0 , Width, height );}  Public Rectangle ( Int X, Int Y, Int Width, Int  Height ){  This . X =X;  This . Y = Y;  This . Width = Width;  This . Height = Height ;}...} 


This class has a series of constructor methods, each of which is initialized.RectangleDepartment variable. If no initialization value is provided for the parameter, the constructor provides the default value for each member variable. For example, if there is no parameter constructor, four parameters with 0 values are input, and the constructor of the four parameters is called. There are two constructor methods for the parameters, and two 0 parameters are input, call the construction method of the four parameters. As mentioned before, the compiler determines which constructor to call based on the number and type of parameters.

 

If so, the call to another constructor must be the first line in the constructor.

Related Article

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.