在java中使用关键字“this”

我试图了解java关键字实际上做了什么。 我一直在阅读Sun的文档,但是我仍然对this实际上做的事情很模糊。

this关键字是对当前对象的引用。

 class Foo { private int bar; public Foo(int bar) { // the "this" keyword allows you to specify that // you mean "this type" and reference the members // of this type - in this instance it is allowing // you to disambiguate between the private member // "bar" and the parameter "bar" passed into the // constructor this.bar = bar; } } 

另一种思考的方式是this关键字就像你用来引用自己的人称代词。 其他语言对于相同的概念有不同的词汇。 VB使用Me和Python约定(因为Python不使用关键字,只是每个方法的一个隐式参数)就是使用self

如果你想引用本质上是你的对象,你可以这样说:

我的arm或我的

this想成是一种types说“我的”的方法。 所以一个伪代码表示如下所示:

 class Foo { private int bar; public Foo(int bar) { my.bar = bar; } } 

这个关键字在不同的环境中可能意味着不同的事物,这可能是您混淆的根源。

它可以用作引用当前方法被调用的实例的对象引用: return this;

它可以用作引用当前构造函数创build的实例的对象引用,例如访问隐藏的字段:

 MyClass(String name) { this.name = name; } 

它可以用来从构造函数中调用一个类的不同构造函数:

 MyClass() { this("default name"); } 

它可以用来访问嵌套类中的封闭实例:

 public class MyClass { String name; public class MyClass { String name; public String getOuterName() { return MyClass.this.name; } } } 

“this”是对当前对象的引用。

在这里看到细节

关键字this是对当前对象的引用 。 最好用下面的代码解释一下:

 public class MyClass { public void testingThis() { // You can access the stuff below by // using this (although this is not mandatory) System.out.println(this.myInt); System.out.println(this.myStringMethod()); // Will print out: // 100 // Hello World } int myInt = 100; string myStringMethod() { return "Hello World"; } } 

除非你在你的地方有代码标准,告诉你使用this关键字,否则它不会被使用太多。 有一个常见的用法,那就是如果你遵循一个代码约定,你的参数名称与你的类属性相同:

 public class ProperExample { private int numberOfExamples; public ProperExample(int numberOfExamples) { this.numberOfExamples = numberOfExamples; } } 

正确使用this关键字是链构造函数(使构造函数的构造对象一致):

 public class Square { public Square() { this(0, 0); } public Square(int x_and_y) { this(x_and_y, x_and_y); } public Square(int x, int y) { // finally do something with x and y } } 

这个关键字在C#中的工作方式相同。

更好的使用这个

 public class Blah implements Foo { public Foo getFoo() { return this; } } 

它允许你在当前的上下文中具体“这个”对象。 另一个例子:

 public class Blah { public void process(Foo foo) { foo.setBar(this); } } 

你还可以做这些操作吗?

“this”关键字指的是当前对象,由于该方法正在执行中。 它也被用来避免当方法variables和局部variables具有相同的名称时,作为方法中的parameter passing的本地variables和实例variables之间的歧义。

示例::

 public class ThisDemo1 { public static void main(String[] args) { A a1=new A(4,5); } } class A { int num1; int num2; A(int num1) { this.num1=num1; //here "this" refers to instance variable num1. //"this" avoids ambigutiy between local variable "num1" & instance variable "num1" System.out.println("num1 :: "+(this.num1)); } A(int num, int num2) { this(num); //here "this" calls 1 argument constructor within the same class. this.num2=num2; System.out.println("num2 :: "+(this.num2)); //Above line prints value of the instance variable num2. } } 

关键字“ this ”是指当前对象的上下文。 在许多情况下(正如Andrew指出的那样),您将使用明确的这个来清楚地表明您指的是当前对象。

另外,从“ 这个和超级 ”:

*还有其他用途。 有时,在编写实例方法时,需要将包含该方法的对象作为实际parameter passing给子例程。 在这种情况下,您可以将其用作实际参数。 例如,如果你想打印出对象的string表示,你可以说“System.out.println(this);”。 或者你可以在赋值语句中将这个值赋给另一个variables。

事实上,你可以用任何其他variables来做任何事情,除了改变它的值。

该网站还提到了“ 超级 ”的相关概念,这可能有助于理解这些如何与inheritance工作。

它是同一个类的一个方法内的一个类的实例的引用。 编码

 public class A{ int attr=10; public int calc(){ return this.getA()+10; } /** *get and set **/ }//end class A 

calc()体内,软件在当前分配的对象内部运行一个方法。

对象的行为如何可能看到自己? 正好用这个关键字。

真的, 这个关键字不需要强制使用(如超级 ),因为JVM知道在哪里调用内存区域的方法,但在我看来,这使得代码更具可测性。

它也可以用来访问当前上下文中的信息。 例如:

 public class OuterClass { public static void main(String[] args) { OuterClass oc = new OuterClass(); } OuterClass() { InnerClass ic = new InnerClass(this); } class InnerClass { InnerClass(OuterClass oc) { System.out.println("Enclosing class: " + oc + " / " + oc.getClass()); System.out.println("This class: " + this + " / " + this.getClass()); System.out.println("Parent of this class: " + this.getClass().getEnclosingClass()); System.out.println("Other way to parent: " + OuterClass.this); } } } 

从英文angular度来看,“这个对象”是你现在拥有的对象。

 WindowMaker foo = new WindowMaker(this); 

例如,您当前正在从JFrame扩展的类中,并且您想要将引用传递给JFrame的WindowMaker对象,以便它可以与JFrame交互。 您可以通过将其引用传递给名为“this”的对象来传递对JFrame的引用。

每个对象都可以使用关键字this访问对自身的引用(有时称为this引用)。

首先让我们看看代码

 public class Employee { private int empId; private String name; public int getEmpId() { return this.empId; } public String getName() { return this.name; } public void setEmpId(int empId) { this.empId = empId; } public void setName(String name) { this.name = name; } 

}

在上面的方法getName()返回实例variables的名字。 现在让我们再看看类似的代码是

 public class Employee { private int empId; private String name; public int getEmpId() { return this.empId; } public String getName() { String name="Yasir Shabbir"; return name; } public void setEmpId(int empId) { this.empId = empId; } public void setName(String name) { this.name = name; } public static void main(String []args){ Employee e=new Employee(); e.setName("Programmer of UOS"); System.out.println(e.getName()); } 

}

输出 Yasir Shabbir

  • 这个操作符总是使用实例variables(属于对象)而不是任何类variables(属于类)
  • 这总是引用类非静态属性而不是任何其他参数或局部variables。
  • 这总是使用非静态方法
  • 这个运算符不能在静态variables(Classvariables)上​​工作

**注意:**当一个方法包含一个与该类的字段名称相同的参数或局部variables时,通常会出现逻辑错误。 在这种情况下,如果您希望访问类的字段,请使用引用this,否则将引用方法参数或局部variables。

什么“这个”很简单。 它拥有当前对象的参考。

  • 该关键字包含当前类的实例的引用
  • 此关键字不能在静态函数或静态块中使用
  • 这个关键字可以用来访问实例的shadowedvariables
  • 这个关键字可以用来在函数调用中传递当前对象作为参数
  • 这个关键字可以用来创build构造链

来源: http : //javaandme.com/core-java/this-word