【问题标题】:Use of "this" keyword in java [duplicate]在java中使用“this”关键字[重复]
【发布时间】:2012-01-03 05:15:02
【问题描述】:

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

【问题讨论】:

  • 并非总是如此。可能有几种情况。例如,当您想将自己的实例作为参数传递给方法时,您可以使用method(this)
  • @Junior Bill Gates:这只是“this”关键字的一种有效用法。

标签: java


【解决方案1】:

this 是实例中当前实例的别名或名称。它对于区分实例变量和局部变量(包括参数)很有用,但它本身可以用来简单地引用成员变量和方法,调用其他构造函数重载,或者简单地引用 instance。适用用途的一些示例(不详尽):

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(...) 必须是第一个语句。
  • 诸如this 之类的限定符或静态调用的类名使您的代码更具可读性并节省大量时间。在一秒钟内,读者就知道变量或方法的范围或其来源。如果没有使用适当的限定符,我拒绝阅读/帮助解决代码问题。我没有时间关注这些方法或猜测它们属于哪个类,特别是如果现代 ide(如 eclipse)在保存操作上有自动完成功能。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    现在,碰巧this 也可用于区分实例数据和构造函数或方法的参数(如果它们恰好相同)。

    【讨论】:

      【解决方案4】:

      这个关键字有以下用途
      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.this关键字可用于调用当前类方法(隐式)

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

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

      6.this也可以用来返回当前类实例

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:

              如果你有关于 c、c++ 或指针的知识,在那种语言中,这是一个指向对象本身的指针。在java中,一切都是参考。所以它是在java中对自身的引用。这个关键字的需求之一是:

              认为这是你的课

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

              如果没有 this 关键字你会混淆这是参数或类字段。当你使用 this.myVar 时它引用这个对象的字段。

              【讨论】:

                【解决方案9】:

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

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

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

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

                【讨论】:

                  【解决方案10】:

                  这在java中使用。我们可以在继承中使用,也可以在方法重载和方法覆盖中使用。因为实际参数或实例变量名称具有相同的名称,所以我们可以使用这个关键字 complsary 。但有时这与我们不能使用这个关键字时不同...... 例如:- 超级类 { 诠释 x; 超级(整数 x) { 这个.x=x } }

                  【讨论】:

                    猜你喜欢
                    • 2010-10-09
                    • 2011-01-26
                    • 2012-04-15
                    • 2011-10-10
                    • 2019-11-18
                    • 1970-01-01
                    • 2010-10-25
                    • 2020-05-01
                    相关资源
                    最近更新 更多