this关键字

先看下面的代码

 

代码
 
   
public class Point {
int x ,y;

public Point( int a, int b) {
x
= a;
y
= b;
}

public void output() {
System.out.println(
" x = " + x);
System.out.println(
" y = " + y);
}

public void output( int x , int y) {
x
= x; // 我们的想法是将参数x赋值给成员变量x
y = y; // 我们的想法是将参数y赋值给成员变量y
// the assignment to variable x has no effect.
// the assignment to variable y has no effect.
}

public static void main(String[] args) {
Point pt;
pt
= new Point( 5 , 5 ); // 调用带参数的构造函数

pt.output(
6 , 6 );
pt.output();
}
}

分析:其实这正如the assignment to variable x has no effect所说,装让无效果。

 

 
  
public void output( int x , int y) {
x
= x;
y
= y;
}

中,中,output方法中的参数名称与成员变量的名称相同,但是在output方法内部看不见成员变量,x无法指向成员变量x,只能看见局部变量,在output方法内部所能看到的所有x都是我们局部变量形参的x,所有的y也是我们局部变量形参的y。所以是把x的值赋值给了x,把y的值赋值给了y。自己赋给自己,当然对成员变量的x和y当然就没有作用了。

 

当然改写成如下就可以实现:

 
  
public void output( int a , int b) {
x
= a;
y
= a;
}

 

但是我们确实就是想要为了方便,将局部变量x改变成员变量x,将局部变量y改变成员变量y。该怎么办呢?使用this关键字就可以解决。

 

this 是指向对象本身。this 变量代表对象本身,也就是当我们去构造完一个pt对象的时候,它就有一个隐含的变量this代表了pt对象本身。当我们再次去构造一个pt2对象的时候,那么它就有了另外一个this代表了pt2对象本身。

 

代码
 
   
public class Point {
private int x ,y;

/* public Point(int a,int b) {
x = a;
y = b;
}
*/

public Point( int x, int y) {
this .x = x;
this .y = y;
}

public void output() {
System.out.println(
" x = " + x);
System.out.println(
" y = " + y);
}

/* public void output(int x ,int y) {
x = x;// 我们的想法是将x赋值给成员变量x
y = y;// 我们的想法是将y赋值给成员变量y
// the assignment to variable x has no effect.
// the assignment to variable y has no effect.
}
*/

public void output( int x , int y) {
this .x = x; // 代表将形参x的值赋值给对象的变量x
this .y = y;
}

public static void main(String[] args) {
Point pt;
pt
= new Point( 5 , 5 ); // 调用带参数的构造函数

pt.output(
6 , 6 );
pt.output();
}
}

 

this的作用:

 当类中有两个同名变量,一个属于类(类的成员变量),而另一个属于某个特定的方法(方法中的局部变量),使用this区分成员变量和局部变量。

使用this简化构造函数的调用。

例如:

 

代码
 
   
public class Point {
int x ,y;

public Point() {
this ( 4 , 4 );
}

public Point( int x, int y) {
this .x = x;
this .y = y;
}

public void output() {
System.out.println(
" x = " + x);
System.out.println(
" y = " + y);
}

public void output( int x , int y) {
this .x = x; // 代表将形参x的值赋值给对象的变量x
this .y = y;
}

public static void main(String[] args) {

Point pt
= new Point();
pt.output(
5 , 5 );
pt.output();
}
}

 

获取当前对象的信息。

例如:

 

代码
 
   
public class Test2 {
public void outputThis() {
System.out.println(
this );
}

public String toString() {
return " 大家好 " ;
}

public static void main(String[] args) {
Test2 t
= new Test2();
t.outputThis();
}
}
// this --> t --> toString()

使用this常犯的错误:

 

  在一个构造函数使用this时,它的前面不能有任何操作语句。例如:

 

 
  
public Point() {
System.out.println(
" this前面有操作 " );
this ( 4 , 4 );
}

 

 

转载于:https://www.cnblogs.com/meng72ndsc/archive/2010/12/20/1911563.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值