解释“此"功能的作用关键字和通过使用"this"隐藏实例变量的概念被称为“隐藏". Java中的关键字 [英] Explain the functioning of "this" Keyword and the concept of Instance Variable Hiding by using "this" keyword in java

查看:149
本文介绍了解释“此"功能的作用关键字和通过使用"this"隐藏实例变量的概念被称为“隐藏". Java中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从 Java 2:完全参考,第5版中学习 Java .我不明白目的this关键字的确切含义以及实例变量隐藏的概念.请举个例子给我解释.

I have been learning Java from Java 2: Complete Reference, 5th Edition. I couldn't understand the exact of purpose this keyword and the concept of instance variable hiding. please explain me with example.

推荐答案

The exact purpose of this is to remove ambiguity from local variable from your field variables.

是实例中当前实例的别名或名称.它对于消除实例变量与局部变量(包括参数)的区别很有用,但它本身可以用来简单地引用成员变量和方法,调用其他构造函数重载或简单地引用实例.适用用途的一些示例(并非详尽无遗):

this is an alias or a name for the current instance inside the instance. It is useful for disambiguating instance variables from locals (including parameters), but it can be used by itself to simply refer to member variables and methods, invoke other constructor overloads, or simply to refer to the instance. Some examples of applicable uses (not exhaustive):

class Foo
{
 private int bar; 

 public Foo() {
      this(42); // invoke parameterized constructor
 }

 public Foo(int bar) {
     this.bar = bar; // disambiguate 
 }

 public void frob() {
      this.baz(); // used "just because"
 }

 private void baz() {
      System.out.println("whatever");
 }

}

还请阅读此关键字以及 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆