java中关于this关键字的一些用法
1、当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量。(this是当前对象自己)
1 public class Hello { 2 String str = "你好"; 3 4 public Hello(String str) { 5 System.out.println("str = " + str); 6 System.out.println("参数赋值给成员变量前this.str = " + this.str); 7 this.str = str;//把参数值赋给成员变量,成员变量的值改变 8 System.out.println("参数赋值给成员变量后this.str = " + this.str); 9 } 10 11 public static void main(String[] args) { 12 Hello hello = new Hello("大家好"); 13 System.out.println("str = " + hello.str);//验证成员变量值的改变 14 } 15 }
运行结果:
在这个例子中,构造函数Hello中,参数str与类Hello的成员变量str同名,这时如果直接对str进行操作则是对参数str进行操作。若要对类Hello的成员变量str进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数str进行打印结果; 第二行是对成员变量str的打印;第三行是先对成员变量str赋传过来的参数str值后再打印,所以结果是大家好,而第四行是主函数中直接打印类中的成员变量的值,这种用法是开发中最常见的情况,在生成表的实体类DTO中会经常看到。
2、把自己当作参数传递时,也可以用this。(this作当前参数进行传递)
1 class A { 2 public A() { 3 new B(this).print();// 调用B的方法 4 } 5 public void print() { 6 System.out.println("大家好,我是小A!"); 7 } 8 } 9 10 class B { 11 A a; 12 public B(A a) { 13 this.a = a; 14 } 15 public void print() { 16 a.print();//调用A的方法 17 System.out.println("大家好,我是大B!"); 18 } 19 } 20 21 public class Hello { 22 public static void main(String[] args) { 23 A a = new A(); 24 a.print(); 25 B b = new B(a); 26 b.print(); 27 } 28 }
运行结果:
在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。
3、有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。
1 public class Hello { 2 int i = 1; 3 int sum = 0; 4 public Hello() { 5 Thread thread = new Thread() { 6 public void run() { 7 for (int i=0;i<20;i++) { 8 //Hello.this.i = 0;//在这里,直接用this.i是取不到类Hello的成员变量i的 9 Hello.this.sum+=i; 10 Hello.this.run();//调用外部类方法(这里因为外部类和内部类的方法同名,所以直接用this是无法调用外部类的run方法的) 11 try { 12 sleep(1000); 13 } catch (Exception e) { 14 e.printStackTrace(); 15 } 16 } 17 } 18 }; // 注意这里有分号 19 thread.start(); 20 } 21 public void run() { 22 System.out.println("i = " + i); 23 i++; 24 System.out.println("sum = " + sum); 25 } 26 27 public static void main(String[] args) throws Exception { 28 new Hello(); 29 } 30 }
运行结果:
i = 1
sum = 0
i = 2
sum = 1
i = 3
sum = 3
i = 4
sum = 6
i = 5
sum = 10
i = 6
sum = 15
i = 7
sum = 21
i = 8
sum = 28
i = 9
sum = 36
i = 10
sum = 45
......
在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。
4、在构造函数中,通过this可以调用同一类中别的构造函数。
1 public class Hello { 2 public Hello(String str) { 3 System.out.println(str); 4 } 5 public Hello() { 6 this("Hello World!"); 7 } 8 public static void main(String[] args) { 9 Hello hello = new Hello(); 10 } 11 }
运行结果:
1 public class Hello { 2 private int age; 3 private String str; 4 public Hello(String str) { 5 this.str=str; 6 } 7 public Hello(String str,int age) { 8 this(str); 9 this.age=age; 10 } 11 public static void main(String[] args) { 12 Hello hello = new Hello("Hello World!",336); 13 System.out.println(hello.str); 14 System.out.println(hello.age); 15 } 16 }
运行结果:
1 public class Hello { 2 private String name; 3 private int age; 4 private String addr; 5 public Hello(String name) { 6 this.name=name; 7 } 8 public Hello(String name,int age) { 9 //在构造调用另一个构造函数,调用动作必须置于最起始的位置。 10 //不能在构造函数以外的任何函数内调用构造函数。 11 this(name); 12 this.age=age; 13 } 14 public Hello(String name,int age,String addr) { 15 //在一个构造函数内只能调用一个构造函数。 16 this(name,age); 17 this.addr=addr; 18 } 19 public static void main(String[] args) { 20 Hello hello = new Hello("野原新之助",5,"东京都港区六本木6丁目"); 21 System.out.println(hello.name); 22 System.out.println(hello.age); 23 System.out.println(hello.addr); 24 } 25 }
运行结果:
5、this同时传递多个参数。
1 public class Hello { 2 String x; 3 String y; 4 public static void sayHello(Hello hello) {//实例化对象 5 System.out.println(hello.x + "给" + hello.y + "打招呼!"); 6 } 7 public void show() { 8 //sayHello(this):这里的this就是把当前实例化的hello传给了sayHello()方法。 9 sayHello(this); 10 } 11 public static void main(String[] args) { 12 Hello hello = new Hello(); 13 hello.x = "大雄"; 14 hello.y = "静香"; 15 hello.show(); 16 } 17 }
运行结果: