通常情况下,我只在构造函数中使用this
。
我知道它用于通过使用this.something
来标识参数变量(如果其与全局变量名称相同)。
然而,我不知道this
在Java中的真正含义以及如果我没有使用点号(.
),会发生什么。
通常情况下,我只在构造函数中使用this
。
我知道它用于通过使用this.something
来标识参数变量(如果其与全局变量名称相同)。
然而,我不知道this
在Java中的真正含义以及如果我没有使用点号(.
),会发生什么。
this
指的是当前对象。
每个非静态方法都在对象的上下文中运行。所以如果你有一个像这样的类:
public class MyThisTest {
private int a;
public MyThisTest() {
this(42); // calls the other constructor
}
public MyThisTest(int a) {
this.a = a; // assigns the value of the parameter a to the field of the same name
}
public void frobnicate() {
int a = 1;
System.out.println(a); // refers to the local variable a
System.out.println(this.a); // refers to the field a
System.out.println(this); // refers to this entire object
}
public String toString() {
return "MyThisTest a=" + a; // refers to the field a
}
}
调用 new MyThisTest()
的 frobnicate()
方法将打印:
1 42 MyThisTest a=42
因此,你可以用它来实现以下多个功能:
this.
如何工作这些基础概念还有所欠缺。我的理解是,this.
允许您调用一个方法或变量,该方法或变量是类的实例化版本所特有的,从而允许另一个版本的方法或变量存在,而不需要调用this.
。在一个简单的名为Test.class
的类中,我有两个方法:public static void main()
和public Test()
。我无法在这两个方法之间传递信息,因为main
是静态的,构造函数不能被定义为静态的。我应该发布一个新问题吗? - dbconfessionprintln
有几个重载版本。如果你使用一个对象调用它,而这个对象不匹配任何其他版本,那么将会调用println(Object)
版本。该版本被指定为在参数上调用toString()
(通过String.valueOf()
)(如果它不是null
)。这是一个常见的主题:当Java需要一个对象的字符串表示形式,并且没有指定其他内容时,通常会在对象上调用toString()
。 - Joachim Sauerthis
关键字的所有不同用途:
定义:Java的this
关键字用于引用其所在方法的当前实例。
以下是使用此关键字的方法:
明确指定实例变量而不是静态或局部变量。也就是说,
private String javaFAQ;
void methodName(String javaFAQ) {
this.javaFAQ = javaFAQ;
}
这里的“this”指的是实例变量。在这里,本地变量的优先级较高。因此,没有使用this
表示本地变量。如果局部变量(参数名称)与实例变量不同,则无论是否使用this
,都表示实例变量。
this
用于引用构造函数。
public JavaQuestions(String javapapers) {
this(javapapers, true);
}
这会调用具有两个参数的相同Java类的构造函数。
this
用于将当前的Java实例作为参数传递。
obj.itIsMe(this);
与上面类似,这也可以用来返回当前实例。CurrentClassName startMethod() {
return this;
}
注意:在上述两种情况下在内部类中使用可能会导致不良结果,因为这将引用内部类而不是外部实例。
this
可用于获取当前类的句柄。
Class className = this.getClass(); // this methodology is preferable in java
虽然这是可以通过以下方法实现的:
Class className = ABC.class; // here ABC refers to the class name and you need to know that!
像往常一样,this
与它的实例相关联,在静态方法中无法使用。
this
也可以用来指代外部对象。class Outer {
class Inner {
void foo() {
Outer o = Outer.this;
}
}
}
public Object getMe() {
return this;
}
this
的一个常见用例是防止变量遮蔽。看下面的例子:
public class Person {
private final String name;
public Person(String name) {
// how would we initialize the field using parameter?
// we can't do: name = name;
}
}
this
允许我们访问此实例的成员,包括该字段。public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
}
引用 programming.guide 上的 文章:
this
在Java程序中有两种用途。
在这种情况下,语法通常如下所示:
this.someVariable = someVariable;
MyClass() {
this(DEFAULT_VALUE); // delegate to other constructor
}
MyClass(int value) {
// ...
}
ActionListener
的类,并将当前实例(即“this”)添加为组件的ActionListener,是相当常见的。public class MyDialog extends JDialog implements ActionListener
{
public MyDialog()
{
JButton myButton = new JButton("Hello");
myButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Hurdy Gurdy!");
}
}
这实际上是“当前上下文中对象的引用”。例如,要打印出“此对象”,您可以编写:
System.out.println(this);
this.variableName
,那么根据定义,它不是全局变量-它是特定于此特定实例的变量。class A {
public boolean is(Object o) {
return o == this;
}
}
A someA = new A();
A anotherA = new A();
someA.is(someA); // returns true
someA.is(anotherA); // returns false
this关键字用于引用块的当前变量,例如考虑下面的代码(只是一个例子,不要期望标准JAVA代码):
Public class test{
test(int a) {
this.a=a;
}
Void print(){
System.out.println(a);
}
Public static void main(String args[]){
test s=new test(2);
s.print();
}
}
就这样,输出结果将会是"2"。 如果我们没有使用this关键字,那么输出结果将会是: 0
对象具有从类派生的方法和属性(变量),为了指定哪些方法和变量属于特定的对象,使用this
保留字。在实例变量的情况下,理解隐式参数和显式参数之间的区别非常重要。看一下audi
对象的fillTank
调用。
Car audi= new Car();
audi.fillTank(5); // 5 is the explicit parameter and the car object is the implicit parameter
public class Car()
{
private double tank;
public Car()
{
tank = 0;
}
public void fillTank(double gallons)
{
tank = tank + gallons;
}
}