Java-'this'运算符 [英] Java - 'this' operator

查看:102
本文介绍了Java-'this'运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的this运算符有疑问.如果程序员会编写这样的代码:

I have a question about the this operator in Java. In the case where a programmer would write a code like this:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}

最好避免阴影效应,并这样做:

Would it be better to avoid shadowing effect and go with this:

private int counter;

public Incrementor(int startValue){
counter = startValue;
}

this运算符是否会使此运算符在以后的编程执行中过时?

Wouldn't the this operator make the this operator obsolete in future programming execrises?

推荐答案

关键字"this"非常有用.您的示例很清楚,但请设想一下您必须将自引用传递给外部对象的另一种方法的情况.

The "this" keyword is very useful. Your example is clear, but imagine the situation in which you have to pass a self reference to another method of an external object.

这篇关于Java-'this'运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆