“这个” java查询点

发布于 2024-10-26 04:36:20 字数 121 浏览 1 评论 0原文

这里是一位休闲 Android 开发人员,我猜这只是一个简单的查询。

我想知道“this”在 java/Android 中到底意味着什么。有时您会将其视为某些方法的固有部分,但我很好奇它到底具体指的是什么。谢谢!

Recreational Android developer here, with what I would guess is a simple query.

I am wondering exactly what "this" means in java/Android. Occasionally you see it as an inherent part of some methods, but I am curious what, in detail, it really refers to. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萤火眠眠 2024-11-02 04:36:20

在我看来,这是:

  • 五十个 Java 关键字之一
  • 特殊(例如只读)对当前对象的引用

您可以在四个中使用它不同的上下文:

  1. 指向当前对象的字段方法 (this.)
  2. 指向当前对象本身(例如Object object = this ;)
  3. 在另一个构造函数中调用构造函数 (this())
  4. (限定 this)指向(非静态)内部类中的外部对象(例如 OuterClassName.this.OuterClassField)

为了更好地理解您需要一些示例:

class Box {
   // Implementing Box(double width = 1, double length = 1, double height = 1):
   Box(double width, double length, double height) {         
      this.width = width; // width is local parameter
      this.length = length; // this.length is object's field
      this.height = height;
   }
   Box(double width, double length) {
      // no statements here allowed
      this(width, length, 1);
      // you can call at most one constructor (recursion not allowed)
   }
   Box(double width) {
      this(width, 1, 1);
   }
   Box() {
      this(1, 1, 1);
      System.out.println("I am default constructor");
   }
   public double getWidth() {
       return this.width; // explicit way (width means the same)
       // return Box.this.width; // full-explicit way
   }
   public void showWidth() {
       System.out.println(this.getWidth());
   }
   public void showWidthAlternate() {
       Box box = this; // the same as explicitly Box box = Box.this;
       // this = box; // can't touch me (read-only reference)
       System.out.println(box.width);
   }
   private double width, length, height;
}

更多信息:

From my view this is:

  • one of fifty Java keywords
  • special (e.g. read-only) reference to the current object

You can use it in four different contexts:

  1. to point to the current object's fields and methods (this.)
  2. to point to the current object itself (e.g. Object object = this;)
  3. to invoke constructor in an another constructor (this())
  4. (Qualified this) to point to the outer object in (not static) inner class (e.g. OuterClassName.this.OuterClassField)

To get better understanding you need some example:

class Box {
   // Implementing Box(double width = 1, double length = 1, double height = 1):
   Box(double width, double length, double height) {         
      this.width = width; // width is local parameter
      this.length = length; // this.length is object's field
      this.height = height;
   }
   Box(double width, double length) {
      // no statements here allowed
      this(width, length, 1);
      // you can call at most one constructor (recursion not allowed)
   }
   Box(double width) {
      this(width, 1, 1);
   }
   Box() {
      this(1, 1, 1);
      System.out.println("I am default constructor");
   }
   public double getWidth() {
       return this.width; // explicit way (width means the same)
       // return Box.this.width; // full-explicit way
   }
   public void showWidth() {
       System.out.println(this.getWidth());
   }
   public void showWidthAlternate() {
       Box box = this; // the same as explicitly Box box = Box.this;
       // this = box; // can't touch me (read-only reference)
       System.out.println(box.width);
   }
   private double width, length, height;
}

More information:

樱&纷飞 2024-11-02 04:36:20

this is used to refer to the current class instance. See http://www.javabeat.net/qna/16-what-is-super-and-this-keyword-in-java/

何必那么矫情 2024-11-02 04:36:20

当前对象

this 指的是在字段被隐藏时主要使用的

,例如:

class Example {

int x;

public void setSomething(int x) {
 this.x = x;
}

}

this.x 指的是Example 类中x 的实例,而不是传递到方法中的x。

如果您想了解更多信息,我编辑添加了链接:

此关键字

this refers to the current object

used mostly when a field is shadowed

for example:

class Example {

int x;

public void setSomething(int x) {
 this.x = x;
}

}

this.x is refering to the instance of x in the Example class not the x passed into the method.

I edited to add the link if you want to read more:

this keyword

夏夜暖风 2024-11-02 04:36:20

它指的是调用该方法的类的实例。

例如,如果您有:

Cow buddy;
...
buddy.moo();

如果“moo()”方法在其定义中使用“this”,它将引用奶牛“buddy”。

It refers to the instance of the class on which the method was called.

For example, if you have:

Cow buddy;
...
buddy.moo();

If the "moo()" method uses "this" inside of its definition, it will refer to the Cow "buddy".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文