用Java中的方法使用“this”怎么样?它是可选的还是有需要使用它的情况?
我遇到的唯一情况是在类中调用方法中的方法。但它是可选的。这是一个愚蠢的例子,只是为了表明我的意思:
public class Test {
String s;
private String hey() {
return s;
}
public String getS(){
String sm = this.hey();
// here I could just write hey(); without this
return sm;
}
}
您需要它的三个明显情况:
以下是这三个例子:
public class Test
{
int x;
public Test(int x)
{
this.x = x;
}
public Test()
{
this(10);
}
public void foo()
{
Helper.doSomethingWith(this);
}
public void setX(int x)
{
this.x = x;
}
}
我相信也有一些奇怪的情况使用内部类,你需要super.this.x
但他们应该避免,因为非常模糊,IMO :)
编辑:我想不出任何为什么你想要它直接this.foo()
方法调用的例子。
编辑:saua在晦涩的内部类例子上做出了贡献:
我认为晦涩的情况是:
OuterClass.this.foo()
从具有foo()
方法的Inner类中的代码访问外部类的foo()
时。
我使用“this”来澄清代码,通常作为我正在调用实例方法而不是访问类级方法或字段的提示。
但不是。除非由于范围命名冲突而需要消除歧义,否则实际上并不需要“这个”。
对于大多数通用编程,this
关键字是可选的,通常用于避免混淆。但是,有一些地方需要它。
class Foo {
int val;
public Foo(int val) {
this(val, 0); //this MUST be here to refer to another constructor
}
public Foo(int val, int another) {
val = val; //this will work, but it generally not recommended.
this.val = val; //both are the same, but this is more useful.
method1(); //in a Foo instance, it will refer to this.method1()
this.method1(); //but in a Foo2 instance, you must use this to do the same
}
public void method1() {}
}
class Foo2 extends Foo {
public Foo2(int val) {
this(val); //this will refer to the other Foo2 constructor
}
public Foo2(int val, int another) {
super(val, another);
super.method1(); //this will refer to Foo.method1()
}
@Override
public void method1() {}//overridden method
}
这些并非所有情况,而是一些更普遍的情况。我希望这有助于您更好地理解this
和super
关键字以及如何/何时使用它们。
唯一真正需要的是当你有一个与成员变量同名的方法的参数时。就个人而言,我尝试始终使用它来使变量/方法的范围显式化。例如,您可以使用静态方法或实例方法。阅读代码时,了解哪个是有用的。
不是答案(所以可以随意投票),但我无法将其纳入有人问的评论中。
很多人使用“this.x”来直观地区分实例变量和局部变量和参数。
所以他们会这样做:
private int sum;
public int storeSquare (int b) {
int c=b*b;
this.sum+=c; // Makes sum "pop" I guess
return c;
}
我个人认为这是一个坏习惯:任何可用的编辑器都会为您提供不同颜色的实例和局部变量 - 它不需要任何人为错误的模式。
用“这个”做到这一点。只有50%的安全。当x是一个局部变量时,如果你试图把this.x放在一起,编译器会抓住它,但没有什么可以阻止你“忘记”用这个来标记一个实例变量。如果你忘了标记只有一个(或者如果其他人在你的代码上工作)并且你依赖于这个模式,那么这个模式可能会比那个更具破坏性
就个人而言,我很确定这种模式源于程序员(合法)对这种情况的不适:
public void setMe(int me) {
this.me=me;
}
事实上你需要“这个”。在我面前是由参数的名称决定的 - 我同意它只是感觉马虎。你希望保持一致 - 如果你需要这个。在我面前,为什么不总是使用它?
虽然我明白了这种不适,但输入了这个。使用实例变量的每个地方都只是迂腐,毫无意义,丑陋和不可靠。如果它真的困扰你并且你绝对需要使用模式来解决它,那就试着把“p”放在你的参数前面。作为副作用,它甚至应该使它更加恒定,因为参数大小写现在将与方法案例匹配。
public void setMe( int pMe)
在方法调用前添加this
的唯一原因是表明您正在调用非静态方法。我想不出任何其他有效理由这样做(纠正我,我错了)。我不推荐这个约定,因为它没有增加太多价值。如果没有一致地应用那么它可能会产生误导(因为这种无需方法的调用仍然可以是非静态方法)。如果被调用的方法是静态的,那么多久一次关心?此外,大多数IDE将以不同方式突出显示静态方法。
我听说过this
表示调用子类的方法的惯例,而this
的缺席是调用超类的方法。但这只是愚蠢的,因为惯例可能是另一种方式。
编辑:正如mmyers指出的那样(见评论),this
使用静态方法。有了这个,我认为绝对没有理由在this
前面,因为它没有任何区别。
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
如果您的方法需要返回对象的实例,则绝对需要这个。
public class StringBuildable {
public StringBuildable append(String text) {
// Code to insert the string -- previously this.internalAppend(text);
return this;
}
}
这允许您以下列方式将方法链接在一起:
String string = new StringBuildable()
.append("hello")
.append(' ')
.append.("World")
.toString()
;