彻底理解Java中this指针
每次看到Java中的this指针,总摸不着头绪。在网上看了很多人的讲解,还是不知道this指针到底是什么东西,今天的的这篇日志可以让你看清this到底是谁。(内容摘自:http://www.mathcs.emory.edu/~cheung/Courses/170.2010/Syllabus/03/implicit-param.html)
抽象的讲,this指针就是一个Implicit Parameter,那到底什么是Implicit Parameter,请看下文详解。
Implicit Parameter
- Parameters to an instance method
- Recall that a parameter is a value that is given to a method as input - see: click here
- Methods can have one or more parameters
- You can pass one or more inputs into a method by specifying the value as parameters when you invoke the method.
- Methods can have one or more parameters
- We have learned that instance methods have two different kinds of parameters:
- Explicit parameter that is passed by specifying the parameter in the parenthesis of a method call.
- Implicit parameter that is passed by specifying an object variable (object reference) before the name of a method.
- Explicit parameter that is passed by specifying the parameter in the parenthesis of a method call.
- Recall that a parameter is a value that is given to a method as input - see: click here
- Explicit parameter
- Review:
The parameters between the brackets/parenthesis of the method call are called explicit parameters - Examples: the values in red are explicit parameters
harrysChecking.deposit(500); harrysChecking.withdraw(amount); box.translate(10, 20); double x = 4.0, y = 6.0; box.translate(x, y);
- NOTE: these statement must be contained in some method, and most likely, they will be in different methods (because their actions are kinda "unrelated". I have omitted the method for brevity - just want to show you how to identify explicit parameters
- Review:
- Accessing an explicit parameter variable inside the method
- Accessing an explicit parameter variable inside a method is very simple:
- Example:
public void deposit(double amount) { balance = balance + amount; }
(Because this is so trivial, I did not dwell on it when we discuss the implementation of the methods.
I dwell on it now because I will show you how to access the implicit parameter variable next.)
- Accessing an explicit parameter variable inside a method is very simple:
- Implicit parameter
- Review:
The object variable before the method name in the method call are called implicit parameters - The object on which you invoke a method is also as a parameter in the method call, because if you use a different object in the method call, the operation is performed on a different object (i.e., the behavior of the method is modified):
harrysChecking.deposit(500); momsSaving.deposit(500);
The first call updates the balance instance variable in harrysChecking, while the second call updates the balance instance variable in momsSaving
- Review:
- Accessing the Implicit parameter inside the method
- There is exactly ONE implicit parameter variable in every method.
- Java has assigned a special name (a keyword) to identify this implicit parameter variable
- The keyword that Java (and C++) uses to refer to the implicit parameter variable is called this
It must be written with all lower case letters
- The obvious question that you will be asking is:
Question:
- What value does the implicit parameter variable this contain ???
- You can figure the answer to this question by using analogy...
Consider the following two pieces of program fragments:
public void deposit(double amount) { balance = balance + amount; }
(1) harrysChecking.deposit(700); (2) momsSaving.deposit(500);
Question:
What is the value of the (explicit parameter variable) amount in the case (1) ?
Question:
And what is the value of the (explicit parameter variable) amount in the case (2) ?
- OK, it's time to apply the analogy:
Question:
What is the value of the (implicit parameter variable) this in the case (1) ?
Question:
And what is the value of the (implicit parameter variable) this in the case (2) ?
Here is a pictorial representation on what's going on inside the computer when momsSaving.deposit(500) is called:
- As you know, object variables such as harrysChecking and momsSaving are used to locate the instance variables of an object - see: click here
Since the object variable (harrysChecking in case (1) and momsSaving in case (2)) is passed to the implicit parameter variable this in the method call, the method can use this to obtain the correct instance variables !!!
(That's how the magic works....)
- Important Fact:
The implicit parameter variable this is an object reference variable and is used to locate the instance variables of the object - It remains to show you how the implicit parameter variable this is used...
Example:
public void deposit(double amount) { balance = balance + amount; }
public void deposit(double amount) { this.balance = this.balance + amount; }
- Notice that the variable balance is an instance variable in the object that is being referenced by the implicit parameter variable this
- Java assumes that a variable name that does not any a name of a parameter variable or a local variable must refer to an instance variable
That's why you don't need to write this before the variable balance
- Notice that the variable balance is an instance variable in the object that is being referenced by the implicit parameter variable this
- There is exactly ONE implicit parameter variable in every method.
- A case that necessitate the use of this
- It is rare that you need to use the implicit parameter variable this
- Here is one case where it is necessary to use this, but it is only so, because of a very bad choice of nomenclature:
- Example where you need to use this in the method:
public void deposit(double amount) { balance = balance + amount; }
public void deposit(double balance) { balance = balance + balance; }
- The deposit() on the right will not work properly....
You can fix this by using this:
public void deposit(double balance) { this.balance = this.balance + balance; }
- It is rare that you need to use the implicit parameter variable this
- DEMO Program: (BankAccount class with this)
- BankAccount.java file: click here
- BankAccountTester.java file: click here
- Compile with: javac BankAccountTester.java
- Run with: java BankAccountTester
- Now edit BankAccount.java and remove the this., compile and run the program again...
The method deposit() can no longer increase the balance in a BankAccount object...
- Question: WHY NOT ???
The name balance matches the name of a parameter variable and thus, the name balance refers to the parameter variable and not to the instance variable