Java Face object keyword this super

Source: Internet
Author: User

This:this is a pointer to the object itself that points inside a member function to the object of the current class

In fact , this mainly to three kinds of usage:

1. Represents a reference to the current object!

2, the use of class member variables, rather than function parameters, note that the function parameters and member variables with the same name are differentiated! In fact, this is the first use of the special case, more commonly used, so that come out to emphasize.

3, used in the construction method to reference the constructor that satisfies the specified parameter type (in fact, the constructor method). But there must be a lot of attention here: only one construction method can be referenced and must be at the beginning!

There is also note: Thiscannot be used in the static method! So even the definition of a static method is: There is no method for this! Although exaggerated, it is fully explained that this cannot be used in the static method!

Package test;

Public class Thistest {

Private int i=0;

//First constructor: has an int type parameter

Thistest (int i) {

        this .i=i+1; At this point this represents the reference member variable I, not the function parameter I

       system. out .println ( "Int constructor i--this.i:    "+i+ "--" + this i);

System. out . println ("i-1:"+ (i-1) +"this.i+1:"+ (this. ) I+ 1));

//From the results of the two outputs fully proves that I and this.i are not the same!

}

//Second constructor: There is a string shape parameter

Thistest (String s) {

System. out . println ("String constructor:"+s);

}

//Third constructor: has an int type parameter and a string shape parameter

Thistest (int i,string s) {

This (s); This invokes a second constructor

//this (i);

This . i=i++; This to reference the member variable of the class

System. out . println ("Int constructor:"+i+"/n"+"String constructor:"+s);

}

Public thistest Increment () {

This . i+ +;

return This ; //Returns the current object that belongs to (thistest)

}

Public Static void Main (string[] args) {

Thistest tt0=New Thistest (ten);

Thistest tt1=New thistest ("OK");

Thistest tt2=New thistest ("OK again!" );

System. out . println (Tt0.increment (). Increment (). Increment (). I);

//tt0.increment () returns a thistest object that is based on the tt0 i + +,

//Then returns the Thistest object on the basis of the object returned above i++ !

}

}

Operation Result:

Int Constructor i--this.i:10--11

String Constructor:ok

String Constructor:ok again!

Int constructor:21

String Constructor:ok again!

14

indicate under what circumstances this is required:
First, the use of this call another constructor method, with the hair is this (parameter list), this is only in the construction method of the class, other places can not be used.
Second, in the case of a function parameter or local variable in a function with the same name as a member variable, the member variable is masked, and the member variable needs to be referenced by the "this. Member variable name" method to access the member variable. Of course, in the absence of the same name, you can directly use the name of the member variable, instead of this, use is not wrong, hehe.
Third, in the function, it is necessary to refer to the current object of the class that the letter belongs to, directly with this.

Super

use super in the Java class to refer to the composition of the parent class, use this to refer to the current object , and if a class inherits from another class, when we new the instance object of the subclass, there will be a parent class object inside the subclass object. How do you refer to the parent object inside? Using Super to refer to, this refers to a reference to the current object, and super is a reference to the parent object inside the current object.

1.1.super keyword Test
 1 package cn.galc.test;         2 3/** 4 * Parent Class 5 * @author GACL 6 * 7 */8 class Fatherclass {9 public int value;10 public void f () {11 Value=100;12 System.out.println ("The Value property of the Parent class =" +value); 13}14}15 16/**17 * Subclass ChildClass inherit from parent class Fatherclass 1 8 * @author gacl19 *20 */21 class ChildClass extends Fatherclass {22/**23 * Subclass In addition to inheriting the Valu attribute that the parent class has, it declares a Val UE Property, 24 * That is, the subclass at this point has two value properties. */26 public int value;27/**28 * In subclass ChildClass overrides the implementation of the F () method inherited from the parent class, which is the method body that overrides the F () method. */30 public void F () {super.f ();//Use Super as a reference object of the parent class to invoke the F () method inside the parent class object value=200;//This value is a child The Valu, which is defined by the class itself, is not the value33 System.out.println inherited from the parent class ("value of the child class =" +value), and System.out.println (value); The print is a subclass of the value of the values of the custom, this value is 20035/**36 * Printed out is the value of the parent class, because the subclass in the rewrite from the parent class F () method inherited down, 37 * The first sentence "Super.f ();” is to have the reference object of the parent class call the F () method of the parent class object, 38 * that is, the parent object calls the F () method itself to change the value of its Value property, from 0 to 100. 39 * TheThe value to print out here is 100. */41 System.out.println (Super.value); 42}43}44 45/**46 * Test class * @author gacl48 *49 */50 Pub         Lic class Testinherit {a public static void main (string[] args) {childclass cc = new ChildClass (); 53 CC.F (); 54}55}

Operation Result:

  

1.2. Draw a memory analysis diagram to understand the whole process of program execution

Analyzing any program starts with the first sentence of the main method, so first analyze the first sentence in the Main method:

Chlidclass cc = new Chlidclass ();

When the program executes here, first in the stack space will produce a variable cc,cc inside the value is what this is not good to say, in a word, through this value we can find the new Chlidclass object. Because subclass Chlidclass is inherited from the parent class Fatherclass, when we new a subclass object, the subclass object contains a parent object, and the parent object has its own property value. This value member variable is declared in the Fatherclass class and does not initialize it, so the system defaults to initialize it to 0, the member variable (declared within the class) can not initialize it when declaring, the compiler will automatically initialize this member variable, But a local variable (declared inside a method) must be initialized at the time of declaration, because the compiler does not automatically initialize the local variable, and any variable must be initialized before it is used.

Subclasses inherit the Value property of the parent class at the same time, they also define a value property individually, so when we new a subclass object, this object will have two value properties, one is inherited from the parent class value, and the other is its own value. The member variable defined in the subclass value is not initialized to it at the time of declaration, so the compiler defaults to initialize it to 0. Therefore, after executing the first sentence, the layout of the system memory is as follows:

 

Next, execute the second sentence:

1 cc.f ();

When the new object comes out, the object produces a reference to this, which points to the object itself. If the new object is a subclass object, then there is a super reference inside the subclass object, which points to the parent object inside the current object. So the equivalent of a program inside a this,this point to the object itself, there is a super,super point to the current object inside the parent object.

Here is called after overriding the F () method, the first sentence of the method body: "Super.f ();" is to let the parent object inside the subclass object call its own f () method to change the value of its Value property, and the parent object calls his own F () method by pointing to his reference super, so after executing this sentence, the value of values inside the parent object becomes 100. Then execute "value=200;" The vaule here is the value that the subclass object declares itself, not the value inherited from the parent class. So after this sentence has been executed, the subclass object itself value is changed to 200. The memory layout at this point is as follows:

The last three sentences in the body of the method are the commands to print the value values, and the first two lines are printed with the value of the subclass object itself, so the printed result is 200, and the last Word prints the parent object's own value of the subclass object, and prints out the result of 100.

In this case, the entire memory analysis is over, and the final memory display results as shown above.

Java Face object keyword this super

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.