English 中文(简体)
在Java中什么时候使用这个关键字?[副本]
原标题:When to use this keyword in Java? [duplicate]
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
Java - when to use this keyword

用这个引用类属性通常是个好约定吗。即使默认情况下会引用该属性?或者我应该只在绝对必要的时候使用它?

这将使程序员更清楚地了解情况,并避免在程序员添加任何具有相同名称的新变量时出现任何令人困惑的错误。

最佳回答

有些人有一个约定,让私有成员变量在前面或后面加下划线。在这些情况下,很明显this是隐含的。

如果您没有私有成员的命名约定,那么this是指示您正在读/写的变量是成员变量的好方法。

我不希望任何具有不同作用域(参数、成员、局部变量)的变量共享相同的名称。我通过为成员命名的惯例来避免这种情况。

问题回答

Use it in copy constructors and other instances where you must. Eg:

Foo(Foo rhs) {
this.bar = rhs.bar;
this.baz = rhs.baz;
}

大多数现代IDE都会对成员变量进行颜色编码,因此您可以将它们与args区分开来。或者,您可以使用一种命名方案,让您知道成员是什么,例如在前面加上m_。

最好不要将args命名为与成员变量相同的名称,这样就不会遇到这样的冲突。

高。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签