在java中使用“this”关键字

当ai遇到this关键字时,我正在研究Java中的方法覆盖。 在互联网和其他来源上搜索了很多这个之后,我得出结论,当实例变量的名称与构造函数参数相同时,使用this关键字。 我是对还是错?

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

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

this关键字可用于(它不能与静态方法一起使用):

  1. 获取通过其调用该方法的对象的引用(实例方法)。
  2. 避免由方法或构造函数参数遮蔽的字段。
  3. 调用同一类的构造函数。
  4. 在重写方法的情况下, this用于调用当前类的方法。
  5. 引用内部类。 例如ClassName.this
  6. 创建内部类的对象,例如enclosingObjectReference.new EnclosedClass

你是对的,但这只是一个使用场景,而不是一个定义。 this关键字引用“当前对象”。 它主要用于使对象可以将自身作为参数传递给另一个对象的方法。

因此,例如,如果有一个名为Person的对象和一个名为PersonSaver的对象,并且您调用了Person.SaveYourself() ,则Person可能只执行以下操作: PersonSaver.Save( this ) ;

现在,它恰好也可以用于消除构造函数或方法的实例数据和参数之间的歧义,如果它们恰好相同的话。

此关键字具有以下用途
1.用于引用当前类实例变量

  class Student{ int id; String name; student(int id,String name){ this.id = id; this.name = name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); s1.display(); s2.display(); } } 

这里参数和实例变量是相同的,这就是我们使用它的原因
2.用于调用当前类构造函数

 class Student{ int id; String name; Student (){System.out.println("default constructor is invoked");} Student(int id,String name){ this ();//it is used to invoked current class constructor. this.id = id; this.name = name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student e1 = new Student(111,"karan"); Student e2 = new Student(222,"Aryan"); e1.display(); e2.display(); } } 

3.这个关键字可以用来调用当前的类方法(隐式)

4.这可以在方法调用中传递参数

5.这可以在构造函数调用中传递参数

6.这也可以用于返回当前的类实例

这是指当前对象 。 如果你有类变量int A和类的方法xyz类的一部分有int A,只是为了区分你引用的’A’,你将使用this.A. 这仅是一个示例案例。

 public class Test { int a; public void testMethod(int a) { this.a = a; //Here this.a is variable 'a' of this instance. parameter 'a' is parameter. } } 

通常,’this’的用法是保留用于变量和方法的实例,而不是类方法……

“类方法不能使用this关键字,因为没有实例可以引用…”

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

这是一个微不足道的例子……

 public class Person { private String name; private int age; private double weight; private String height; private String gender; private String race; public void setName( String name ) { this.name = name; } public String getName() { return this.name; } public void setAge( int age) { this.age = age; } public int getAge(){ return this.age; } public void setWeight( double weight) { this.weight = weight; } public double getWeight() { return this.weight; } public void setHeight( String height ) { this.height = height; } public String getHeight() { return this.height; } public void setGender( String gender) { this.gender = gender; } public String getGender() { return this.gender; } public void setRace( String race) { this.race = race; } public String getRace() { return this.race; } public void displayPerson() { System.out.println( "This persons name is :" + this.getName() ); System.out.println( "This persons age is :" + this.getAge() ); System.out.println( "This persons weight is :" + this.getWeight() ); System.out.println( "This persons height is :" + this.getHeight() ); System.out.println( "This persons Gender is :" + this.getGender() ); System.out.println( "This persons race is :" + this.getRace() ); } } 

而对于一个人的例子….

 public class PersonTest { public static void main( String... args ) { Person me = new Person(); me.setName( "My Name" ); me.setAge( 42 ); me.setWeight( 185.00 ); me.setHeight( "6'0" ); me.setGender( "Male" ); me.setRace( "Caucasian" ); me.displayPerson(); } } 

如果你对c,c ++或指针有所了解,那么在这种语言中,这是一个指向对象本身的指针。 在java中,一切都是引用。 所以它在java中引用自身。 此关键字的一个需求是:

认为这是你的class级

 public class MyClass { public int myVar; public int myMethod(int myVar) { this.myVar = myVar; // fields is set by parameter } } 

如果没有这个关键字,你会感到困惑,这是参数或类字段。当你使用this.myVar时,它引用了这个对象的字段。

如果成员变量和局部变量名冲突,这个关键字可以用来引用成员变量,如,

 public Loan(String type, double interest){ this.type = type; this.interest = interest; } 

我想修改你的语言。 当您需要在构造函数中使用类全局变量时,将使用this关键字。

 public class demo{ String name; public void setName(String name){ this.name = name; //This should be first statement of method. } } 

是对当前对象的引用 – 正在调用其方法或构造函数的对象。 您可以使用方法从实例方法或构造函数中引用当前对象的任何成员。

还有一点需要注意的是, 关键字可能是您方法的第一个语句。

这在java中使用。 我们可以在inheritance中使用,也可以在方法重载和方法覆盖中使用。 因为实际参数或实例变量名具有相同的名称,所以我们可以使用此关键字complsary。 但有些时候这与我们不能使用这个关键词complsary时不一样…..例如: – class super {int x; super(int x){this.x = x}}