JAVA basis (xv) this keyword

This representative of this class;

This features:

           . This method calls a member method of the present members of the class, the method may also call the parent class;

           This (parameter list) call is another constructor of the current class, can only be used between the constructor is only executed once

          This () must be placed on the first line

          We can add this in front of the member variable name. To distinguish between members and local variables

Own understanding: Who to call this, this represents whom.

 

{Class Student Public 

 String name; // define a member variable name

   // no-argument constructor

    Student(){

    }

   // constructor to initialize the name of

     Student(String nm) {

        name = nm;

    }

 Private  void SetName (String name) { // definition of a parameter (local variable) name 
  //this.name represent the class name, 
// since already constructed a method for initializing the name = name nm ; it is only necessary to call     
// call other constructors, you need to call by this keyword

  the this .name = name; // the value of the local variable passed to the member variable


 }

}
Two variable names if the same words if this plus a keyword in front of a variable, which refers to the member variable or method of the object, 
rather than a form member method parameters or local variables

In a Java class, which methods can be divided members methods and constructors two kinds.

             And the method is a constructor class of the same name, there is a need in the Java class constructor.

      If no constructor shown embodied in the code, then the compiler will automatically add a constructor parameter does not form at compile time. 
The constructor method with the ordinary members still have a lot of different places.
So this keyword can not only be used to refer to member variables, but also can be used to reference the constructor.
Returns the value of the object
In addition to this key reference variable or member method, there are a significant role is a reference to return to class. 
The code can be used return this, returns a reference to a class. At this time, the keyword this represents the name of the class.
As the code above student class, then the meaning of the code is representative of return student.
Yet, this key addition to this reference variable or member method, you can also type as the return value, this is the most interesting place this keyword.
 

 

Guess you like

Origin www.cnblogs.com/layuechuquwan/p/11302118.html