Java 此关键字用作引用变量
这是一个保存当前对象的引用ID的变量。 那么为什么它不能用作参考变量呢Java 此关键字用作引用变量,java,this,Java,This,这是一个保存当前对象的引用ID的变量。 那么为什么它不能用作参考变量呢 Temp t = new Temp(); //Temp be any class t.show(); //show() be any method in Temp this.show(); // error 如果您所在的类没有show()方法,或者您试图从静态上下文中执行此操作,则只会引发错误 此保留当前对象的引用ID,因此它取决于您所在的位置,而不是刚刚创建的对象。仅当您所在的类没有
Temp t = new Temp(); //Temp be any class
t.show(); //show() be any method in Temp
this.show(); // error
如果您所在的类没有
show()
方法,或者您试图从静态上下文中执行此操作,则只会引发错误
此
保留当前对象的引用ID,因此它取决于您所在的位置,而不是刚刚创建的对象。仅当您所在的类没有show()
方法或者您试图从静态上下文执行此操作时才会引发错误
此
保留当前对象的引用ID,因此它取决于您所在的位置,而不是刚刚创建的对象。仅当您所在的类没有show()
方法或者您试图从静态上下文执行此操作时才会引发错误
此
保留当前对象的引用ID,因此它取决于您所在的位置,而不是刚刚创建的对象。仅当您所在的类没有show()
方法或者您试图从静态上下文执行此操作时才会引发错误
此
保留当前对象的引用ID,因此它取决于您所在的位置,而不是刚刚创建的对象。当前对象不是您上次引用的对象。下面是这种用法的示例:
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
当前对象不是您上次引用的对象。下面是这种用法的示例:
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
当前对象不是您上次引用的对象。下面是这种用法的示例:
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
当前对象不是您上次引用的对象。下面是这种用法的示例:
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
java中的“this”关键字用于引用代码运行的对象。
在比较对象本身时,它最有用。例如:
public boolean equals(Object object) {
return object == this;
}
下面是帮助您理解的另一段代码:
public class Test {
public Test() {
}
public static void main(String... args) {
Test test = new Test();//original test object
Test test2 = new Test();
test.equals(test);//returns true
test.equals(test2);//returns false
}
public void equals(Test testParameter) {
// in this particular case "this" refers to the
// original test object (defined in the main method)
// as it is the only object calling this method.
return testParameter == this; // "this" is the object that is calling the method
}
}
java中的“this”关键字用于引用代码运行的对象。
在比较对象本身时,它最有用。例如:
public boolean equals(Object object) {
return object == this;
}
下面是帮助您理解的另一段代码:
public class Test {
public Test() {
}
public static void main(String... args) {
Test test = new Test();//original test object
Test test2 = new Test();
test.equals(test);//returns true
test.equals(test2);//returns false
}
public void equals(Test testParameter) {
// in this particular case "this" refers to the
// original test object (defined in the main method)
// as it is the only object calling this method.
return testParameter == this; // "this" is the object that is calling the method
}
}
java中的“this”关键字用于引用代码运行的对象。
在比较对象本身时,它最有用。例如:
public boolean equals(Object object) {
return object == this;
}
下面是帮助您理解的另一段代码:
public class Test {
public Test() {
}
public static void main(String... args) {
Test test = new Test();//original test object
Test test2 = new Test();
test.equals(test);//returns true
test.equals(test2);//returns false
}
public void equals(Test testParameter) {
// in this particular case "this" refers to the
// original test object (defined in the main method)
// as it is the only object calling this method.
return testParameter == this; // "this" is the object that is calling the method
}
}
java中的“this”关键字用于引用代码运行的对象。
在比较对象本身时,它最有用。例如:
public boolean equals(Object object) {
return object == this;
}
下面是帮助您理解的另一段代码:
public class Test {
public Test() {
}
public static void main(String... args) {
Test test = new Test();//original test object
Test test2 = new Test();
test.equals(test);//returns true
test.equals(test2);//returns false
}
public void equals(Test testParameter) {
// in this particular case "this" refers to the
// original test object (defined in the main method)
// as it is the only object calling this method.
return testParameter == this; // "this" is the object that is calling the method
}
}
除非您不在Temp
类中,或者您在Temp
类中,但在静态上下文中,否则不会引发错误。除非您不在Temp
类中,或者您在Temp
类中,但在静态上下文中,否则不会引发错误。除非您不在Temp
类中,或者您在静态上下文中,否则不会引发错误在Temp
类中,但在静态上下文中。除非您不在Temp
类中,或者您在Temp
类中,但在静态上下文中,否则不会引发错误。那么“当前对象”是什么意思?@anmolsharma将其视为您所在的类。那么“当前对象”是什么意思呢意思是?@anmolsharma认为它是你所在的类。那么“当前对象”是什么意思?@anmolsharma认为它是你所在的类。那么“当前对象”是什么意思?@anmolsharma认为它是你所在的类。