refer to the online information and self-understanding summarize the respective usage and differences of this and super in Java
< A;. Usage of this
The constructor is an important way to create a Java object, and when the constructor is called with the New keyword, the constructor returns the object for that class, but the opponent is not entirely created by the constructor. Creating an object is divided into the following four steps:
A. Allocating object space and initializing the object member variable to 0 or null
B. Performing an explicit initialization of a property value
C. Implementing a construction method
D. Returning the address of an object to a related variable
The essence of this is "the address of the created object" because the object was created before the method call was constructed. Therefore, you can also use this in the constructor method to represent the current object.
The description of this in the book
This represents a reference to an object that points to the object that is executing the method. In particular, in the constructor method, when other construction methods are called through the This keyword, they must be placed in the first row, or the compiler will make an error. And in the construction method, you can only call another construction method through this.
This example one
PackageCn.sxt.oop; Public classTest_this_super {intID; String name; BooleanMan ; Doublewidth; PublicTest_this_super () {System.out.println ("Parameterless constructor"); } PublicTest_this_super (intnumber,string name) { This(); System.out.println ("Passing int and string form parameter constructors"); } PublicTest_this_super (intNumber,string name,Double_width) { This(Number,name); This. width =_width; System.out.println ("Pass int, String, double form parameter constructor"); } PublicTest_this_super (intNumber,string name,Chara) {System.out.println ("Pass int, String, char form parameter constructor"); } Public Static voidMain (string[] args) {test_this_super T1=NewTest_this_super (); Test_this_super T2=NewTest_this_super ("Tom")); test_this_super T3=NewTest_this_super (+, "long", 3.14); test_this_super T4=NewTest_this_super ("Long", ' a '); }}
Print results
T1 parameterless Constructor T2 the parameterless constructor passes an int and string form parameter to the constructor T3 the parameterless constructor passes an int and a string form parameter to the constructor passing int, string, Double form parameter constructor T4 Pass int, String, char form parameter constructs the device
Graphic analysis is as follows;
< two > Super usage
Super is a reference to the parent class, and the compiler automatically adds a default super () method call to the constructor if it does not display the constructor of the parent class. If the parent does not have a default parameterless constructor, the compiler will make an error, and the super () statement must be the first clause of the constructed method.
When you define an object of a subclass, the constructor of the subclass is called first, and then the constructor of the parent class is called, and if the parent class function is sufficient, it will always be called to the final parent class constructor, and the function call will use the stack space, so the constructor of the subclass is first entered in the order of the stack. Then is the neighboring parent class constructor, and finally the top of the stack is the final parent class constructor, and the constructor execution is executed in order from the top of the stack to the bottom of the stack.
Now take bird--> Animal---Object as a line example
Public classAnimal {intEye = 2; PublicAnimal () {Super(); System.out.println (Animal); } Public voidrun () {System.out.println ("Animals have different ways of walking."); } Public Static voidMain (string[] args) {Bird b=NewBird (); B.run (); }}classBirdextendsanimal{ PublicBird () {Super(); System.out.println (Birds); } @Override Public voidrun () {//TODO auto-generated Method Stub Super. Run ();//by super You can use the parent class method and propertiesSystem.out.println ("The bird is flying, flying, flying, flying."); System.out.println ("Birds have" +Super. eye+ "Eyes Only"); } }
Print results
Animal bird animals have different ways of walking birds are flying fly flying flying birds have 2 eyes
Bird--> Animal---Object graphic analysis is as follows
< three > this and super difference
1) Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
2) This (parameter): Invokes another form of the constructor in this class (should be the first statement in the constructor)
3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member function name (argument)
4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)
5) call Super () must be written in the first row of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.
6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
7) Super () and this () must be placed in the first line of the construction method.
8) Although you can call a constructor with this, you cannot call two.
9) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.
This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
11) In essence, this is a pointer to this object, but super is a Java keyword.
This and super usage in Java