Java keyword---The origin of this and its three major functions

Source: Internet
Author: User

"declaration" Welcome reprint, but please keep the original source of the article →_→

Looking: http://www.cnblogs.com/Qinstudy/

Article Source: http://www.cnblogs.com/Qinstudy/p/Qinstudy.html

Body

Looking hard to master bo, hello! I am determined to master the Java programming language of a small white, recently I am learning Java Object-oriented three major features (encapsulation, inheritance, polymorphism), often encounter this keyword, but the book is simply a few of the functions of this, I hear is foggy, It doesn't explain why the Java designer created the This keyword, for what reason did you create the This keyword? And what's the problem with this keyword?

These are my doubts, as to why I ask these questions, because I think the learning concept is not only to learn the concept itself, more importantly, to understand and understand the origin of knowledge. Worship Bowen Xie again----Qin study hard

Java Beginner--Xiao Zhang

Looking's hard answer:

Hello, Xiao Zhang! When I first learned this, I only knew what the This keyword did in the program, but I didn't know how the This keyword was generated. This is a good sentence--learning concept not only to learn the concept itself, more important is to understand and understand the origin of knowledge .

You have raised two questions and I have listed the questions:

1.Java Why did the designer create the This keyword, for what reason did you create the This keyword? In short, the This keyword produces a background

2. What problem does this keyword solve? In short, the role of the This keyword

Inspired by the "more important thing is to understand and understand the origin of knowledge," I went online to consult the relevant information,"Java Programming Ideas" (Fourth edition, 5th Chapter 5.4,p84 page) gave a good answer. Let me tell you the knowledge in the book according to my Own understanding:

This keyword can be understood in English as the demonstrative pronoun this, which is used to refer to something, for example: Look,this is a table. This refers to table.

In the design of the Java language , this also has a pointing relationship .

1. Background of the This keyword

Let me start by answering why the Java designer created the This keyword? I'll use a piece of code to explain why.

1 classperson{2       Public voidspeak () {3           System.out.println ("an object to Invoke method");4     }5     6      Public Static voidMain (string[] args) {7Person p1=NewPerson ();8Person p2=NewPerson ();9 P1.speak (); P1 object to call the Speak () methodTen P2.speak (); P2 object to call the Speak () method

11}

(pro-Test) operation Result:

An object to invoke a method
An object to invoke a method

However, when there is only one speak () method, how does the compiler know if the Speak () method is P1 or is it called by P2? In order to be able to write code with simple, object-oriented syntax-that is, "Send messages to Objects"-the compiler does some behind the scenes work. It secretly passes the reference of the Manipulated object as the first argument to the Speak () method. So the method after the call has become this:

Person speak (P1);

Person speak (P2);

This is an internal form of expression . If we write this, the compiler will give you an error, but it will help you understand what the compiler is doing. Suppose you want to get a reference to the current object inside the method, because the reference is "secretly" passed in by the compiler, so no identifiers are available.

Thus, in order to solve the background problem of getting a reference to the current object within a method, the Java designer specifically designed the This keyword to obtain a reference to the current object within the method. The This keyword can only be used inside a method, representing a reference to the "object that called the method" . It can be said thatthe use of this is not different from the references of other new objects .

Note, however, that if you call another method of the same class inside a method, you can omit this and call it directly. Because the this reference in the current method is automatically applied to other methods in the same class. So, Xiao Zhang you can write code like this, the compiler will not error:

Class person{
public void Speak () {}
public void Eat () {
When you call another method of the same class inside a method, you can omit this and call it directly.
Speak (); The compiler is automatically translated into This.speak ().
}
public static void Main (string[] args) {
}
}

This is why the This keyword was created, Xiao Zhang Ah, in summary, the This keyword produces the background: in order to resolve the ' get a reference to the current object within the method ' issue . The This keyword is designed to get a reference to the current object inside the method.

The role of 2.this keywords

Three major functions:

1) distinguish between local variables and member variables with the same name ,this. Member variable calls the member variable in this class ;

2) Call the methods in this class through this . Method () ;

3)This is a call between the constructor methods.

Xiao Zhang Ah, perhaps you see these three functions, may still be in the fog in the clouds. In your letter, you say that you are learning about the three major features of object-oriented in Java ( encapsulation, inheritance, polymorphism), so I think you already understand the private encapsulation, extends inheritance, and

Know about the concepts of set, get method, and Null parameter, the method of constructing a parameter. The following procedure requires you to have these foundations above, if you do not have these foundations for the time being, do not worry, go first to look at these basics. After you understand these basics, you can look back and see the code below, and you'll have an epiphany!

If you already have these basics, I'll use a complete program to clarify the three functions of the This keyword.

 PackageDay_12;//Person class Public classPerson {PrivateString name; Private intAge ;  PublicPerson (String name) {//the name in String name is a local variable //the role of this 1: distinguish between local variables and member variables with the same name           This. name=name;//the name in THIS.name is a member variable       }       PublicPerson (String name,intAge ) {          //function 3:this The invocation of the keyword between construction methods           This(name);//Use this (name) to invoke the constructor of only one parameter name           This. age=Age ; }             Public voidsetName (String name) { This. name=name; }       PublicString GetName () {return  This. Name; }             Public voidSetage () { This. age=Age ; }       Public intGetage () {return  This. Age; }      }
 PackageDay_12;//Worker Class Public classWorkerextendsPerson { PublicWorker (String name,intAge ) {        Super(Name,age);    Super (Name,age) calls the parent class person's construction method with two arguments. }         Public voidWork () {//role 2: Theuse of this in the method System.out.println ( This. GetName () + "in the chopping tree!" "); }}

Using this.getname () there is the idea of encapsulation inside . Since I do not know who the object calling the GetName () method is, I use the vague concept of which object to invoke the work () method, which is the object that this refers to.

It is clear that the object of the worker class WK to invoke the work () method, which points to the WK object . So use This.getname () to get the properties in the WK object: Bald head strong.

Testing class: Test

 Package Day_12;  Public class Test {   publicstaticvoid  main (string[] args) {       Worker wk=  New Worker ("bald-headed Strong");       Wk.work ();      }}

(pro-Test) operation Result: bald-headed strong in chopping tree !

3. Methodology

Xiao Zhang, I am glad that you can ask such two questions, these two questions prompted me to think about the background of this keyword and the three major functions of this one. After telling you this, I think I have deepened my understanding of this. Xiao Zhang Ah, later in the Java learning process encountered foggy problems, although write to me, as long as I can understand, must be straightforward to speak clearly. Learning a language, more thinking, often summed up is a good state, and you share!

Java keyword---The origin of this and its three major functions

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.