每次看到Java中的this指针,总摸不着头绪。在网上看了很多人的讲解,还是不知道this指针到底是什么东西,今天的的这篇日志可以让你看清this到底是谁。(内容摘自:http://www.mathcs.emory.edu/~cheung/Courses/170.2010/Syllabus/03/implicit-param.html)
抽象的讲,this指针就是一个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.
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
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); |
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
Accessing an explicit parameter variable inside the method
Accessing an explicit parameter variable inside a method is very simple:
To use an explicit parameter variable inside a method, you simply refer the parameter variable by its name
Example:
public void deposit(double 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.)
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); |
The first call updates the balance instance variable in harrysChecking, while the second call updates the balance instance variable in momsSaving
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) | (1) harrysChecking.deposit(700); |
Question:
What is the value of the (explicit parameter variable) amount in the case (1) ?
amount = 700
Question:
And what is the value of the (explicit parameter variable) amount in the case (2) ?
amount = 500
OK, it's time to apply the analogy:
Question:
What is the value of the (implicit parameter variable) this in the case (1) ?
this = harrysChecking !!!
Question:
And what is the value of the (implicit parameter variable) this in the case (2) ?
this = momsSaving !!!
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
Example:
public void deposit(double amount) | public void deposit(double 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 toan instance variable
That's why you don't need to write this before the variable balance
A case that necessitate the use of this
It is rareôthat you need to use the implicitparameter 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:
the name of (one of the) parameter variable线is the same区as the name of the instance variable
Example where you need to use thisདྷin the method:
public void deposit(double amount) | public void deposit(double balance) |
The:deposit() on the right will not work properly....
You can fix this by using this:
public void deposit(double balance) |
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
还没有评论,来说两句吧...