java里的“this”到底是什么意思

我在看thinking in java这本书,this这个概念实在看不太明白,求高手用比较贴切的方式解释一下。谢谢。满意答案 必+50分。
比如有this和没有this的区别?

打个比方?

this代表当前对象的意思。
这个this肯定是用在某个类的方法里吧。呵呵,我们继续往下说。

举个例子:
public class persion{

private String name=null;
private int age;

public void setName(String name){
this.name=name;
}
public String getName(){

return name;
}

}

在这个类中setName方法中第一个变量用this是为了和本方法的参数this做区别,表示这个name指的是Person这个类的name属性,而不是name参数,如果去掉这个this,执行完这个方法后,Person类的name属性仍然为null
getName方法返回的Person类的name,之所以不用this是因为没有必要,因为编译器会知道这个name就指的是Person的name而不是其他的name(因为找个方法没有name变量和它混淆)。当然,这个方法你也可以显示的使用return this.name,没错。
这些都是最基本的了。开始学只要记住这个就可以了。慢慢的其他的用法也就无师自通了!
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-08

this表示类实例本身。

this的用法

1、表示对当前对象的引用!

public class A{
   public A getA(){
      return this;//表示获取当前实例本身
   }
}

2、表示类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!

public class A{
   private int a = 0;//位置1
   public A getA(int a){
      this.a = a;//前面this.a表示 位置1 的a,赋值=号右侧的表示参数a
   }
}

3、用于在构造方法中引用满足指定参数类型的构造器。

public class A{
   public A(int a){
   }
   public A(){ 
     this(1);//这里调用自身的构造函数public A(int a){
   }
}

第2个回答  2010-12-02
this为一系统资源,只允许用户读而不允许写,它存放当前对象的地址(引用)。

this变量有以下作用:

1. 构造方法重用:

public class Rectangle{
public Rectangle(Location at, Shape size) {…}
public Rectangle(Shape size,Location at){
this(at, size); }
public Rectangle(Location at) {
this(at, new Shape(100,100));
}
public Rectangle(Shape size) {
this(size, new Location(1,1));
}
public Rectangle() {
this(new Location(1,1), new Shape(100,100));
}
}

2、消除歧义:

Location{
private int x;
private int y;
public Location(int x,int y) {
this.x=x;
this.y=y;
}
……
}

3、返回对象-链式方法调用:

public class Count {
private int i = 0;
Count increment() {
i++;
return this; //返回对象的地址,所以我们可以链式访问它
}
void print() {
System.out.println("i = " + i);
}
}
public class CountTest{
public static void main(String[] args) {
Count x = new Count();
x.increment().increment().print();
}
}

4、作为参数传递"this”变量-进行回调:

假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如:
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this); //代表你所创建的对象,因为它要用到.
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}

其中我们开发中最常用到的地方是第二点,消除歧义。

比方说有类
public class A
里面有几个变量
private String aa,
private String bb;

this 在这里就代表A ,其实它是对对象A的引用。
我们在用到aa或者bb的时候,this.aa 和 直接用aa 是没有区别的。
但是假如你在某个方法里也有个变量aa,比如:
public void dosomething(String aa){
this.aa = aa;
//这个时候就this.aa 代表对象A中的变量,而不加this的话,代表方法中的变量。
}
第3个回答  2010-12-02
public class Test {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
//如果不加this,输出null
name = name;
}

public static void main(String[] args) {
Test t = new Test();
t.setName("zhangsan");
System.out.println(t.getName());

}

}

public class Test {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
//如果加this,输出张三
this.name = name;
}

public static void main(String[] args) {
Test t = new Test();
t.setName("zhangsan");
System.out.println(t.getName());

}

}

其实this就表示当前对象,在设置属性的时候,如果变量名相同,可以起到区分的作用
第4个回答  2010-12-02
我记得是专项指示。。。具体的真忘记了。。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网