在java中使用“this”关键字

时间:2012-01-03 05:15:03

标签: java

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

10 个答案:

答案 0 :(得分:13)

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

}

答案 1 :(得分:10)

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

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

答案 2 :(得分:2)

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

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

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

答案 3 :(得分:2)

此关键字具有以下用途
                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.这也可用于返回当前的类实例

答案 4 :(得分:1)

这是指当前对象。如果你有类变量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.
}
}

答案 5 :(得分:1)

通常,'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();
    }
}

答案 6 :(得分:0)

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

认为这是你的班级

public class MyClass
{
      public int myVar;

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

      }


}

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

答案 7 :(得分:0)

如果成员变量和局部变量名冲突,则此关键字可用于引用成员变量,

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

答案 8 :(得分:-1)

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

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

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

还应该记住的另一件事是这个关键字可能是您方法的第一个声明。

答案 9 :(得分:-2)

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