Java语言:"this"关键字用法(在Talend tJavaRow中使用)



talend我正在编写一些小型代码,元数据中的一类。区域,一个可以编码Java类,然后在组件Tjavarow中,可以调用类的方法。

所以当我上课并在tjavarow中写下班级的名称然后是一个点,出现的上下文对话框没有显示方法,但显示了" this this"时,我遇到了这种情况。关键字以及其他事项。我决定使用"这个",然后放一个点,然后上下文对话框出现以显示同类的方法。

我的问题是关键字'this'是否能够将类隐式实例化成一个对象,因此,这就是为什么我能够看到类的方法?

我只是决定将我的一种方法更改为静态方法并以这种方式使用。

因此,如果关键字" this"可以正确地将类实例化到对象的情况下,而无需使用"新"关键字将Java类实例化?

我对此进行了一些研究,我发现了关键字" this"可以做的事情的清单。

此关键字的用法

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的课程,我们有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


<ClassName>.thisthis对象的快捷方式。

由于您在public static void main的静态上下文中,因此您无法从这里访问非静态实例。

您的代码需要实例化类的对象,并使用其非静态方法槽实例对象。

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的使用是对外部类的引用:

public class A {
    class B {
        void x () { A outer = A.this; }
    }
    B create() { return new B(); }
}

在这种情况下,只有从A内部使用B b = new A().create()的使用情况或用于回答您有关上下文的问题

的示例中的实例上下文
A ao = new A();
B b = ao.new B();

也使用className。如果您在两个实例上下文中都有相同的名称,则在匿名和子类上使用此匿名类别。

我想我看到了您在这里的追求。在"当前类"上使用this仍然意味着您需要调用new。从一类中,您可以使用this参考实例成员。("实例"胜于说"当前类",因为您确实需要一个实例,您不能仅使用类类型。(

所以类似:

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 是静态方法,否则您将获得错误,然后将其使用 to 从一种非静态方法会给您警告。

最新更新