简体   繁体   English

类中的this关键字

[英]The this keyword in classes

I am learning Java, and going through the documentation. 我正在学习Java,并正在阅读文档。

There is a line on this page which I am not able to understand - 此页面上有一行我无法理解-

... Also, class methods cannot use the this keyword as there is no instance for this to refer to. ...此外,类方法不能使用this关键字,因为没有可引用的实例。 ... ...

I thought that it was only static class methods which could not use the this keyword. 我以为只是静态类方法不能使用this关键字。

To test this, I wrote the following, which compiles. 为了测试这一点,我编写了以下内容进行编译。

import java.math.*;

class Point {

    public int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public double getDistanceFromOrigin() {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }

}

I have a class all right, in which a method refers to this . 我有一个很好的类,其中一个方法引用了this

Am I misinterpreting things in some way? 我是否以某种方式误解了事情?

Class methods are static methods. 类方法静态方法。 A "class method" is a method that is bound to the class definition (using the static keyword), as opposed to object/instance methods, which you write so that you can call them on objects you build based on that class. “类方法”是绑定到类定义(使用static关键字)的方法,而不是您编写的对象/实例方法,以便您可以在基于该类构建的对象上调用它们。

The code you've written has two object/instance methods, and no class methods. 您编写的代码有两个对象/实例方法,没有类方法。 If you want a class method in Java, you make it static, and then you cannot use this . 如果要使用Java中的类方法,请将其设为静态,然后就不能使用this

I thought that it was only static class methods which could not use the this keyword. 我以为只是static类方法不能使用this关键字。

You're right. 你是对的。 The static methods in a class belong to the class, not to the object reference. 类中的static方法属于该类,而不属于对象引用。 So to prove your sentence, just add a static method and use this keyword in it. 因此,要证明您的句子,只需添加static方法并在其中使用this关键字即可。 For example: 例如:

class Point {

    public int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public double getDistanceFromOrigin() {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }

    public static double getDistanceBetweenPoints(Point point1, Point point2) {
        //uncomment and it won't compile
        //double result = this.x;
        //fancy implementation...
        return 0;
    }

}

您是正确的,只有static类方法不能使用this关键字,但是您的代码示例是非静态的,因此this是完全有效的。

You are using this inside an instance method which will refer to the current instance. 您正在实例方法中使用this方法,该方法将引用当前实例。

public double getDistanceFromOrigin() {
    return Math.sqrt(this.x*this.x + this.y*this.y);
}

If you change the method to a static method , this will not be available , because static methods are tied to the class and not to a particular instance of the class whereas this refers to the current instance of a class if used inside a method. 如果你改变了方法,静态方法, this将无法使用,因为静态方法被绑定到类,而不是类的特定实例,而this在方法内部使用是指一类的当前实例。

public static double getDistanceFromOrigin() {
    return Math.sqrt(this.x*this.x + this.y*this.y); // compilation error here
}

After reading the content at the link you've posted, It seems to use the verbiage Class methods to refer to static methods 阅读了您发布的链接上的内容之后,似乎使用了Verbiage Class methods来引用静态方法。


Class Methods 类方法

The Java programming language supports static methods as well as static variables. Java编程语言支持静态方法以及静态变量。 Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in 静态方法的声明中带有static修饰符,应使用类名调用它们,而无需创建该类的实例,如

You can't use this in static methods because there is no instance(no this ) to reference. 您不能在静态方法中使用this方法,因为没有实例(没有this )要引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM