Java 语言:"this" 关键字用法(用于 Talend tJava行)
Java language : "this" keyword usage (used in Talend tJavaRow)
在 Talend 中,我正在编写一些小代码,class 在元数据“代码”区域中,可以编写 java Class 然后在组件 tJavaRow 中,一个可以调用 class.
的方法
所以我 运行 进入了当我制作 class 并在 tJavaRow 中写入 Class 的名称然后是一个点时的情况,出现的上下文对话框没有显示方法,但显示了“this”关键字以及其他内容。
我决定使用 'this' 然后放一个点,然后出现上下文对话框以显示 class.
中的方法
我的问题是关键字“this”是否能够将 class 隐式实例化为对象,因此这就是为什么我能够看到 class 的方法?
我刚决定将其中一个方法更改为静态方法并以这种方式使用它。
所以如果关键字'this'可以实例化一个class对象是正确的而不用'new'关键字实例化java class到一个对象?
我对此做了一些研究,我发现了关键字 'this' 可以做的事情的列表。
Usage of this keyword
It can be used to refer current class instance variable.
this() can be used to invoke current class constructor.
**It can be used to invoke current class method (implicitly)**
It can be passed as an argument in the method call.
It can be passed as argument in the constructor call.
It can also be used to return the current class instance.
所以放一些代码示例来说明:
假设我们有一个名为 mySphere 的 class,我们有方法 mySurfaceArea 和 myVolume,我们可以这样调用方法吗:
mySphere.this.mySurfaceArea();
mySphere.this.myVolume();
感谢输入!
我刚刚在此基础上创建了自己的代码 运行,但出现错误:
public class MyClass {
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = MyClass.this.myAreaCalc(x);
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(int A){
return A*A;
}
}
Error:
/MyClass.java:9: error: non-static variable this cannot be referenced from a static context
w = MyClass.this.myAreaCalc(x);
^
1 error
我想我明白你在这里的目的了。在 "current class" 上使用 this
仍然意味着您需要调用 new
。在 class 中,您可以使用 this
来引用实例成员。 ("Instance" 比说 "current class" 好,因为你确实需要一个实例,你不能只使用 class 类型。)
所以像这样:
public class MyClass {
private final int area;
public MyClass( int a ) {
area = a;
}
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
MyClass mc = new MyClass( x );
w = mc.myAreaCalc();
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(){
return this.area * this.area;
}
}
我相信您感到困惑的那一行是指我放在下面代码中的这个例子:
public class MyClass {
public MyClass() { //Note this is now a Constructor, not a public static method
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = this.myAreaCalc(x); //These two lines are the same
w = myAreaCalc(x); //These two lines are the same
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(int A){
return A*A;
}
}
在非静态上下文中,您可以使用 this.method()
调用方法,但 this
是隐式调用的,只能用作同一个 Class
中的 method()
].
注意:你不能使用this
from一个静态方法否则你会得到一个错误,并且使用它到从非静态方法调用静态方法会给你一个警告。
<ClassName>.this
是 this
对象的快捷方式,而您在 class 上下文中。
由于您在 public static void main
的静态上下文中,您无法从此处访问非静态实例。
您的代码需要实例化 class 的对象并通过实例对象使用其非静态方法。
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = new MyClass().myAreaCalc(x);
System.out.println("Area Calc is = " + w);
}
在这种情况下<ClassName>.this
的用法是对外部class的引用:
public class A {
class B {
void x () { A outer = A.this; }
}
B create() { return new B(); }
}
在这种情况下,您只能在 A 的实例上下文中使用 B b = new A().create()
在示例中创建 B 的对象或回答有关上下文的问题
A ao = new A();
B b = ao.new B();
如果您在两个实例上下文中具有相同的名称,ClassName.this 在匿名和子 classes 上的用法也用于变量区分。
在 Talend 中,我正在编写一些小代码,class 在元数据“代码”区域中,可以编写 java Class 然后在组件 tJavaRow 中,一个可以调用 class.
的方法所以我 运行 进入了当我制作 class 并在 tJavaRow 中写入 Class 的名称然后是一个点时的情况,出现的上下文对话框没有显示方法,但显示了“this”关键字以及其他内容。 我决定使用 'this' 然后放一个点,然后出现上下文对话框以显示 class.
中的方法我的问题是关键字“this”是否能够将 class 隐式实例化为对象,因此这就是为什么我能够看到 class 的方法?
我刚决定将其中一个方法更改为静态方法并以这种方式使用它。
所以如果关键字'this'可以实例化一个class对象是正确的而不用'new'关键字实例化java class到一个对象?
我对此做了一些研究,我发现了关键字 'this' 可以做的事情的列表。
Usage of this keyword
It can be used to refer current class instance variable.
this() can be used to invoke current class constructor.
**It can be used to invoke current class method (implicitly)**
It can be passed as an argument in the method call.
It can be passed as argument in the constructor call.
It can also be used to return the current class instance.
所以放一些代码示例来说明: 假设我们有一个名为 mySphere 的 class,我们有方法 mySurfaceArea 和 myVolume,我们可以这样调用方法吗:
mySphere.this.mySurfaceArea();
mySphere.this.myVolume();
感谢输入!
我刚刚在此基础上创建了自己的代码 运行,但出现错误:
public class MyClass {
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = MyClass.this.myAreaCalc(x);
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(int A){
return A*A;
}
}
Error:
/MyClass.java:9: error: non-static variable this cannot be referenced from a static context
w = MyClass.this.myAreaCalc(x);
^
1 error
我想我明白你在这里的目的了。在 "current class" 上使用 this
仍然意味着您需要调用 new
。在 class 中,您可以使用 this
来引用实例成员。 ("Instance" 比说 "current class" 好,因为你确实需要一个实例,你不能只使用 class 类型。)
所以像这样:
public class MyClass {
private final int area;
public MyClass( int a ) {
area = a;
}
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
MyClass mc = new MyClass( x );
w = mc.myAreaCalc();
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(){
return this.area * this.area;
}
}
我相信您感到困惑的那一行是指我放在下面代码中的这个例子:
public class MyClass {
public MyClass() { //Note this is now a Constructor, not a public static method
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = this.myAreaCalc(x); //These two lines are the same
w = myAreaCalc(x); //These two lines are the same
System.out.println("Area Calc is = " + w);
}
public int myAreaCalc(int A){
return A*A;
}
}
在非静态上下文中,您可以使用 this.method()
调用方法,但 this
是隐式调用的,只能用作同一个 Class
中的 method()
].
注意:你不能使用this
from一个静态方法否则你会得到一个错误,并且使用它到从非静态方法调用静态方法会给你一个警告。
<ClassName>.this
是 this
对象的快捷方式,而您在 class 上下文中。
由于您在 public static void main
的静态上下文中,您无法从此处访问非静态实例。
您的代码需要实例化 class 的对象并通过实例对象使用其非静态方法。
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
int w;
System.out.println("Sum of x+y = " + z);
w = new MyClass().myAreaCalc(x);
System.out.println("Area Calc is = " + w);
}
在这种情况下<ClassName>.this
的用法是对外部class的引用:
public class A {
class B {
void x () { A outer = A.this; }
}
B create() { return new B(); }
}
在这种情况下,您只能在 A 的实例上下文中使用 B b = new A().create()
在示例中创建 B 的对象或回答有关上下文的问题
A ao = new A();
B b = ao.new B();
如果您在两个实例上下文中具有相同的名称,ClassName.this 在匿名和子 classes 上的用法也用于变量区分。