2009-02-23 130 views
18

我试图理解java关键字this究竟是做什么的。 我一直在阅读Sun的文档,但我仍然对this实际上的功能很模糊。在java中使用关键字“this”

+1

@JasonC问题只能是本网站其他问题的重复。 – 2014-08-18 15:55:41

回答

36

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

如果你要引用本质上是你的目标,你会说,像这样的:this只是一个方式为

手臂或

思考一种说“我的”的类型。因此,一个伪码表示应该是这样的:

class Foo 
{ 
    private int bar; 

    public Foo(int bar) 
    { 
     my.bar = bar; 
    } 
} 
+1

您可以在不使用“this”的情况下进行上述操作,例如使用匈牙利符号等。 m_bar = bar。所以并没有真正解释这是什么真的很重要。 – 2009-02-23 13:18:47

+1

是否可以避免这样的奇怪构造? – Magnar 2009-02-23 13:25:46

+0

@ng:当你不能重命名该字段时,你会做什么?就像它是来自超级班的受保护的领域一样? – 2009-02-23 13:37:06

6

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

见详情here

4

一个更好的利用这一

public class Blah implements Foo { 

    public Foo getFoo() { 
     return this; 
    } 
} 

它可以让你明确“本”对象在当前上下文中。另一个例子:

public class Blah { 

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

你还能怎么做这些操作。

5

The keyword this is a reference to the current object。这是最好用下面的代码解释:

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#。

1

关键字'这个'是指当前对象的上下文。在很多情况下(如Andrew指出),您将使用明确的这个来说明您指的是当前对象。

此外,从 'this and super':

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

事实上,你可以做这样的事情,你可以做任何其他变量,除了改变其值。*

该网站也指“超级”的相关概念,这可能证明有助于理解这些如何与继承工作。

0

从英文角度来看,“这个对象”是你当前拥有的对象。

WindowMaker foo = new WindowMaker(this); 

例如,您目前从JFrame的扩展一个类里面,你想传递给WindowMaker的对象的引用对JFrame,因此它可以与JFrame的交互。您可以通过将引用传递给名为“this”的对象来传递对JFrame的引用。

21

关键字this在不同的情况下可能意味着不同的事情,这可能是您的困惑的来源。

它可以用来作为它是指该实例的当前方法被称为上的对象引用:return this;

它可以用来作为其是指当前构造器创建实例,例如一个对象引用访问隐藏的字段:

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

它可被用于从一个构造中调用AA类的不同构造:

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

它可以被用来访问来自一个嵌套类内包围实例:

public class MyClass 
{ 
    String name; 

    public class MyClass 
    { 
     String name; 

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

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

public class A{ 
    int attr=10; 

    public int calc(){ 
    return this.getA()+10; 
    } 
    /** 
    *get and set 
    **/  

}//end class A 

计算值()体,软件运行当前分配对象内部的方法。

对象的行为可能会如何看待自身?正好与这个关键字。

说真的,这关键字不要求强制使用(如超级),因为JVM知道在哪里调用储存区的方法,但在我看来这使代码更readeable。

1

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

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); 
    } 
    } 
} 
2

“this”关键字是指当前对象由于该方法是下执行。它也用于避免每当实例变量和局部变量具有相同名称时,作为方法中的参数传递的本地变量与实例变量之间的歧义。

例::

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. 
    } 
} 
0

每个对象可以访问到自身的引用与此关键字(有时称为本 参考)。

首先让承担代码

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看看()返回的实例变量名。 现在让我们类似的代码再看看是

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()); 

} 

}

输出 亚西尔Shabbir

  • 该运营商总是以实例变量工作(属于对象) 没有任何类变量(属于类)
  • 这总是指类非静态属性不是任何其他参数或局部变量。
  • 这总是在非静态方法使用
  • 该运营商不能在静态变量工作(类变量)

**注:**这往往是当一个方法包含一个参数或局部变量中的逻辑错误该名称与该类的字段具有相同的名称 。在这种情况下,如果您想访问类的 字段,请使用引用this - 否则将引用方法参数或局部变量。

0

什么'这'很简单。它持有当前 对象的参考。

  • 此关键字持有
  • 此关键字不能静态功能或静止象素块内使用当前类的实例的参考
  • 此关键字可以用于访问实例的阴影可变
  • 此关键字可用于在函数调用中传递当前对象作为参数
  • 此关键字可用于创建构造函数链

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