Java 使用关键字";这";在爪哇

Java 使用关键字";这";在爪哇,java,keyword,Java,Keyword,我试图了解java关键字this的实际作用。 我一直在阅读Sun的文档,但我仍然不清楚这个实际上做了什么。“这”是对当前对象的引用 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

我试图了解java关键字
this
的实际作用。 我一直在阅读Sun的文档,但我仍然不清楚这个实际上做了什么。

“这”是对当前对象的引用

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

请参阅详细信息此
关键字是对当前对象的引用

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

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

class Foo
{
    private int bar;

    public Foo(int bar)
    {
        my.bar = bar;
    }
}
我的胳膊还是腿

可以将
看作是一种类型表示“我的”的方式。因此,伪代码表示形式如下所示:

class Foo
{
    private int bar;

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

更好地利用这一点

public class Blah implements Foo {

   public Foo getFoo() {
      return this;
   }
}
它允许您在当前上下文中指定“this”对象。另一个例子:

public class Blah {

   public void process(Foo foo) { 
      foo.setBar(this);
   }
}
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);
    }
  }
}
您还可以如何执行这些操作。

。最好用以下代码来解释:

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;
    }
}
该关键字的一个正确用法是链接构造函数(使构造对象在整个构造函数中保持一致):

此关键字的工作方式与C#相同。

关键字“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;
    }
}
此外,从“”:

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

事实上,除了改变它的值之外,你可以用这个做任何你可以用其他变量做的事情*


该网站还提到了“super”的相关概念,这可能有助于理解这些概念如何与继承一起工作。

用英语思考,“this object”就是您当前拥有的对象

WindowMaker foo = new WindowMaker(this);

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

关键字
this
在不同的上下文中可能意味着不同的事情,这可能是您混淆的根源

它可以用作对象引用,引用调用当前方法的实例:
returnthis

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

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

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

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

物体的行为如何可能看到自己?用这个关键字,准确地说


实际上,this关键字不需要强制使用(如super),因为JVM知道在内存区域中调用方法的位置,但在我看来,这使代码更易于阅读。

它也可以是访问当前上下文信息的一种方式。 例如:

public class Blah {

   public void process(Foo foo) { 
      foo.setBar(this);
   }
}
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);
    }
  }
}
“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.
    }
}

每个对象都可以使用关键字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()返回实例变量名。 现在让我们再看看类似的代码是什么

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

}
}

输出 亚西尔·沙比尔

  • 此运算符始终使用实例变量(属于对象) 没有任何类变量(属于类)
  • 这总是指类的非静态属性,而不是任何其他参数或局部变量
  • 这通常用于非静态方法
  • 此运算符无法处理静态变量(类变量)
**注:**当方法包含具有 与类的字段同名。在这种情况下,如果您希望访问 字段否则,将引用方法参数或局部变量

“这”的作用非常简单。它持有当前的参考 反对

  • 该关键字保存当前类实例的引用
  • 此关键字不能在静态函数或静态块内使用
  • 此关键字可用于访问实例的隐藏变量
  • 此关键字可用于在函数调用中将当前对象作为参数传递
  • 此关键字可用于创建构造函数链

来源:

您可以在不使用“this”的情况下执行上述操作,例如使用类似匈牙利语的符号。