Java Foundation Object-oriented this keyword
2017-02-14
The core concept of this is:
The This keyword represents the current object in Java.
The main usage of the This keyword:
The This keyword is used in Java to implement the invocation of a class property, the invocation of a class method, and the current object.
First, call properties
1 classbook{//Book class2 PrivateString title;//Title Property3 Private intPrice;//Price Property4 5 Public book (String T, int. p) {6 title = t; 7 price = P; 8}9 Ten Public voidSettitle (String title) { One This. title =title; A } - PublicString GetTitle () { - returntitle; the } - Public voidSetprice (intPrice ) { - This. Price =Price ; - } + Public intGetPrice () { - returnPrice ; + } A PublicString GetInfo () { at return"Title:" +title+ ", Price:" +Price ; - } - } - Public classThisDemo1 { - Public Static voidMain (string[] args) { -Book B =NewBook ("Java Development Fundamentals", 22) ; in System.out.println (B.getinfo ()); - } to}
The main function of the above code is to initialize the properties of the book class, and now the parameters in the constructor are initialized for the properties in the class, so the best way to do this is to set the parameters in the constructor to be consistent with the property names in the class.
1 public book (String title, int price) {2 title = title; 3 Price = Price; 4 }
Modifying the Parameter form above is reasonable, but eventually the content passed into the constructor is not delivered to the attribute.
If the attribute name of the parameter is the same as the parameter name, by default, if no limit is added, the name of the variable in the nearest "{}" is referred to. In order to be able to explicitly find the variable that you want to access belongs to a property in the class, you need to precede the variable with the This keyword.
1 public book (String title, int price) {2 this.title = title; 3 this.price = Price; 4 }
Ii. Method of Invocation
As can be seen from the above, the so-called this actually refers to the properties or methods in this class.
1 classbook2{2 PrivateString name;3 Private intAge ;4 PublicBook2 (String name,intAge ) {5 //Construction Method6 This. SetName (name);7 This. Setage (age);8 }9 Public voidsetName (String name) {Ten This. Name =name; One } A PublicString GetName () { - returnname; - } the Public voidSetage (intAge ) { - This. Age =Age ; - } - Public intGetage () { + returnAge ; - } + Public voidprint () { A //The print method in this class atSystem.out.println ("************"); - } - PublicString GetInfo () {//common methods in this class - This. Print (); - /** - * Use the This keyword to invoke methods in this class, or you can call directly in */ - return"Personal Information" ; to } + -}
For the rigor of the program, be sure to add the This keyword when calling the normal method.
In addition to a common method in a class, a constructor method is included. This time to indicate that multiple construction methods are to be called each other, using the form "This (parameter, parameter)"
1 classbook3{2 PrivateString name;3 Private intAge ;4 PublicBook3 () {//non-parametric construction5 System.out.println ("A new class object has been created");6 }7 PublicBOOK3 (String name) {//There is a construction of a parameter8 System.out.println ("A new class object has been created");9 This. Name =name;Ten } One PublicBOOK3 (String name,intAge) {//construction with two parameters A System.out.println ("A new class object has been created"); - This. Name =name; - This. Age =Age ; the } -}
The above code is implemented regardless of the number of arguments passed in will print a "A new class object produced", but the code has a lot of duplication. Then we need to resolve the duplicated code.
1 classbook3{2 PrivateString name;3 Private intAge ;4 PublicBook3 () {//non-parametric construction5System.out.println ("A new class object has been created");6 }7 PublicBOOK3 (String name) {//There is a construction of a parameter8 This ();9 This. Name =name;Ten } One PublicBOOK3 (String name,intAge) {//construction with two parameters A This (name); - This. Age =Age ; - } the}
Although the above implementation of the construction method calls each other, but there are still some limitations.
1, use this () Form, this () can only be placed in the first line of the method.
2, the construction method can call the ordinary method, but the ordinary method can not call the construction method.
3, when the construction method calls each other, you must keep the exit of the call. This means that at least one construct does not use this () to invoke other constructs when the construction method calls each other.
Third, indicates the current object
1 classbook4{2 Public voidprint () {3 /**4 * That object called the print () method, the This keyword refers to the object
This is the object that is currently calling the method5 */6System.out.println ("this =" + This );7 }8 }9 Public classThisDemo4 {Ten Public Static voidMain (string[] args) { OneBOOK4 B1 =NewBook4 (); ASystem.out.println ("B1 =" +B1); - b1.print (); -System.out.println ("--------------"); theBook4 B2 =NewBook4 (); -System.out.println ("b2 =" +B2); - b2.print (); - } +}
Iv. Summary
1, the property calls in the class will be added after this
2, the construction methods in the class call each other, we must preserve the export.
3, this represents the current object, which is the object that is currently invoking a method in the class. This is not a fixed one.
Java Foundation Object-oriented this keyword