java中this作为参数_java中关于this关键字的一些用法

1、当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量。(this是当前对象自己)

1 public classHello {2   String str = "你好";3

4   publicHello(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 voidmain(String[] args) {12     Hello hello = new Hello("大家好");13     System.out.println("str = " + hello.str);//验证成员变量值的改变

14 }15 }

运行结果:

9064d87776aa0d07107527dc4fc520f4.png

在这个例子中,构造函数Hello中,参数str与类Hello的成员变量str同名,这时如果直接对str进行操作则是对参数str进行操作。若要对类Hello的成员变量str进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数str进行打印结果; 第二行是对成员变量str的打印;第三行是先对成员变量str赋传过来的参数str值后再打印,所以结果是大家好,而第四行是主函数中直接打印类中的成员变量的值,这种用法是开发中最常见的情况,在生成表的实体类DTO中会经常看到。

2、把自己当作参数传递时,也可以用this。(this作当前参数进行传递)

1 classA {2 publicA() {3 new B(this).print();//调用B的方法

4 }5 public voidprint() {6 System.out.println("大家好,我是小A!");7 }8 }9

10 classB {11 A a;12 publicB(A a) {13 this.a =a;14 }15 public voidprint() {16 a.print();//调用A的方法

17 System.out.println("大家好,我是大B!");18 }19 }20

21 public classHello {22 public static voidmain(String[] args) {23 A a = newA();24 a.print();25 B b = newB(a);26 b.print();27 }28 }

运行结果:

390fcf58e69f792e571358e3742c6432.png

在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。

3、有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。

1 public classHello {2 int i = 1;3 int sum = 0;4 publicHello() {5 Thread thread = newThread() {6 public voidrun() {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 voidrun() {22 System.out.println("i = " +i);23 i++;24 System.out.println("sum = " +sum);25 }26

27 public static void main(String[] args) throwsException {28 newHello();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 classHello {2 publicHello(String str) {3 System.out.println(str);4 }5 publicHello() {6 this("Hello World!");7 }8 public static voidmain(String[] args) {9 Hello hello = newHello();10 }11 }

运行结果:

d93ab75d74604780f87275b099891ffe.png

1 public classHello {2 private intage;3 privateString str;4 publicHello(String str) {5 this.str=str;6 }7 public Hello(String str,intage) {8 this(str);9 this.age=age;10 }11 public static voidmain(String[] args) {12 Hello hello = new Hello("Hello World!",336);13 System.out.println(hello.str);14 System.out.println(hello.age);15 }16 }

运行结果:

ed4804f1744a97e61edc86e675cd16db.png

1 public classHello {2 privateString name;3 private intage;4 privateString addr;5 publicHello(String name) {6 this.name=name;7 }8 public Hello(String name,intage) {9 //在构造调用另一个构造函数,调用动作必须置于最起始的位置。10 //不能在构造函数以外的任何函数内调用构造函数。

11 this(name);12 this.age=age;13 }14 public Hello(String name,intage,String addr) {15 //在一个构造函数内只能调用一个构造函数。

16 this(name,age);17 this.addr=addr;18 }19 public static voidmain(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 }

运行结果:

90aad902a06047b2bf627a8e6eb4a4d4.png

5、this同时传递多个参数。

1 public classHello {2 String x;3 String y;4 public static void sayHello(Hello hello) {//实例化对象

5 System.out.println(hello.x + "给" + hello.y + "打招呼!");6 }7 public voidshow() {8 //sayHello(this):这里的this就是把当前实例化的hello传给了sayHello()方法。

9 sayHello(this);10 }11 public static voidmain(String[] args) {12 Hello hello = newHello();13 hello.x = "大雄";14 hello.y = "静香";15 hello.show();16 }17 }

运行结果:

1d7ec0a6dc030b21024ef16bd9e5c63d.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值