java中的this随处可见,用法也多,现在整理有几点:
1. this是指当前对象自己。
当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中:
public class Hello { String s = "Hello"; public Hello(String s){ System.out.println("s = " + s); System.out.println("1 -> this.s = " + this.s); this.s = s; System.out.println("2 -> this.s = " + this.s); } public static void main(String[] args) { Hello x=new Hello("HelloWorld!"); } }
运行结果:
s = HelloWorld! 1 -> this.s = Hello 2 -> this.s = HelloWorld!
在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld!
2. 把this作为参数传递
当你要把自己作为参数传递给别的对象时,也可以用this。如:
public class A { public A() { new B(this).print(); } public void print() { System.out.println("Hello from A!"); } } public class B { A a; public B(A a) { this.a = a; } public void print() { a.print(); System.out.println("Hello from B!"); } }
运行结果:
Hello from A!
Hello from B!
在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。
3. 注意匿名类和内部类中的中的this。
有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子:
public class A { int i = 1; public A() { Thread thread = new Thread() { public void run() { for(;;) { A.this.run(); try { sleep(1000); } catch(InterruptedException ie) { } } } }; //注意这里有; thread.start(); } public void run() { System.out.println("i = " + i); i++; } public static void main(String[] args) throws Exception { new A(); } }
在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。
4。在构造函数中,通过this可以调用同一class中别的构造函数,如
public class Flower{ Flower (int petals){} Flower(String ss){} Flower(int petals, Sting ss){ //petals++;调用另一个构造函数的语句必须在最起始的位置 this(petals); //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数 } }
值得注意的是:
1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。
2:不能在构造函数以外的任何函数内调用构造函数。
3:在一个构造函数内只能调用一个构造函数。
Java关键字this只能用于方法方法体内。当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,这在“Java关键字static、final 使用总结”一文中给出了明确解释。并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。
package test; public class ThisTest { private int i=0; //第一个构造器:有一个int型形参 ThisTest(int i){ this.i=i+1;//此时this表示引用成员变量i,而非函数参数i System.out.println("Int constructor i——this.i: "+i+"——"+this.i); System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1)); //从两个输出结果充分证明了i和this.i是不一样的! } // 第二个构造器:有一个String型形参 ThisTest(String s){ System.out.println("String constructor: "+s); } // 第三个构造器:有一个int型形参和一个String型形参 ThisTest(int i,String s){ this(s);//this调用第二个构造器 //this(i); this.i=i++;//this以引用该类的成员变量 System.out.println("Int constructor: "+i+"/n"+"String constructor: "+s); } public ThisTest increment(){ this.i++; return this;//返回的是当前的对象,该对象属于(ThisTest) } public static void main(String[] args){ ThisTest tt0=new ThisTest(10); ThisTest tt1=new ThisTest("ok"); ThisTest tt2=new ThisTest(20,"ok again!"); System.out.println(tt0.increment().increment().increment().i); //tt0.increment()返回一个在tt0基础上i++的ThisTest对象, //接着又返回在上面返回的对象基础上i++的ThisTest对象! } }
运行结果:
Int constructor i——this.i: 10——11 String constructor: ok String constructor: ok again! Int constructor: 21 String constructor: ok again! 14
细节问题注释已经写的比较清楚了,这里不在赘述,只是总结一下,其实this主要要三种用法:
2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较常用,所以那出来强调一下。
3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。但是这里必须非常注意:只能引用一个构造方法且必须位于开始!
还有就是注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!
说明在什么情况下需要用到this:
第一、通过this调用另一个构造方法,用发是this(参数列表),这个仅仅在类的构造方法中,别的地方不能这么用。
第二、函数参数或者函数中的局部变量和成员变量同名的情况下,成员变量被屏蔽,此时要访问成员变量则需要用“this.成员变量名”的方式来引用成员变量。当然,在没有同名的情况下,可以直接用成员变量的名字,而不用this,用了也不为错,呵呵。
第三、在函数中,需要引用该函所属类的当前对象时候,直接用this。
其实这些用法总结都是从对“this是指向对象本身的一个指针”这句话的更深入的理解而来的,死记不然容易忘记而且容易搞错,要理解!