this指针 java_彻底理解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 moreparameters
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.
Explicitparameter
Review:
The?parameters between the brackets/parenthesisof 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
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)
{
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.)
Implicit?parameter
Review:
The?object variable before the method namein 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
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)?amountin the case (1) ?
amount= 700
Question:
And what is the value of the?(explicit?parameter variable)?amountin the case (2) ?
amount= 500
OK, it's time to apply the analogy:
Question:
What is the value of the?(implicit?parameter variable)?thisin the case (1) ?
this= harrysChecking !!!
Question:
And what is the value of the?(implicit?parameter variable)?thisin 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?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?variablemust refer to?an 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?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:
the name of (one of the) parameter variable?is the sameas the name of the instance variable
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;
}
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 variableand thus, the name?balance?refers to the?parameter variableand?not?to the?instance variable
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的this指针 java_彻底理解Java中this指针的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现123n_java三线程交替
- 下一篇: java并发问题_并发理论基础:并发问题