The This keyword in Java

Source: Internet
Author: User

The 1.this keyword represents the current object

  This . Property Operation Properties of the current object

   This method invokes the current object.

2. The This keyword is often used when encapsulating the properties of an object
 Public classTelphone {Private floatScreen ; Private floatCPU; Private floatMem;  Public voidSendMessage () {System.out.println ("SendMessage"); }         Public floatGetscreen () {returnScreen ; }     Public voidSetscreen (floatScreen ) {         This. screen = screen;//manipulate the properties of the current object         This. SendMessage ();//method that invokes the current object    }     Public floatgetcpu () {returnCPU; }     Public voidSetCpu (floatCPU) {         This. CPU =CPU; }     Public floatGetmem () {returnMem; }     Public voidSetmem (floatmem) {         This. Mem =Mem; }     PublicTelphone () {System.out.println ("Com.imooc.Telphone"); }     PublicTelphone (floatNewscreen,floatNEWCPU,floatNewmem) {        if(newscreen<3.5f) {System.out.println ("..............."); screen= 3.5f; } CPU=newcpu; Mem=Newmem; System.out.println ("----------------"); }}
This

This is an object of its own, representing the object itself, which can be understood as: a pointer to the object itself .

The usage of this can be broadly divided into 3 types in Java:

1. Normal Direct references

This does not have to be said, this is equivalent to pointing to the current object itself.

2. The name of the member who participates in the membership is distinguished by this:

 PackageS2;classPerson {Private intAge = 22;  PublicPerson () {System.out.println ("Age of Initialization:" +Age ); }         Public intGetage (intAge ) {         This. Age = Age;//The name of the member in the form        return  This. Age; }} Public classTest03 { Public Static voidMain (string[] args) {person Harry=NewPerson (); System.err.println ("Harry's age is:" + harry.getage (33)); }}

Operation Result:

Age of initialization:Harry' s aged is:33

3. Reference constructors

Let's talk about this with super, see below

The This keyword in 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.