Summary of super and this usage in Java

Source: Internet
Author: User

First, this usage

Concept: This is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself.

The usage of this can be roughly divided into three types in Java:

  1. Direct reference to ordinary objects: this corresponds to the current object itself.

2. Use this to distinguish between members whose names are duplicate. 

classPerson {Private intAge = 10;  PublicPerson () {System.out.println ("Age of Initialization:" +Age );}  Public intGetage (intAge ) {         This. Age =Age ; return  This. Age; }}  Public classTest1 { Public Static voidMain (string[] args) {person Harry=NewPerson (); System.out.println ("Harry's Age is" +harry.getage (12)); }} 

Operation Result:

Age of initialization: 10
Harry ' s age is 12

 3. Reference constructor: This is similar to super, see the following super function usage.

Second, super usage

  Concept: Super can be understood as a pointer to a super (parent) class object, which refers to a parent class closest to itself.

1. Normal Direct references

  Like this, super is the parent class that points to the current object, so you can use SUPER.XXX to use a parent class that is closest to you.

2. The member variable or method in the subclass has the same name as a member variable or method in the parent class

classCountry {String name; voidvalue () {Name= "China"; }}  classCityextendsCountry {String name; voidvalue () {Name= "Shanghai"; Super. value ();//calling a method of the parent classSystem.out.println (name); System.out.println (Super. Name); }       Public Static voidMain (string[] args) {City C=NewCity ();       C.value (); }}

Operation Result:

Shanghai
China

As you can see, both the methods of the parent class are called and the variables of the parent class are called. If you do not call the parent class method value () and only call the parent class variable name, the parent class name value is the default value NULL.

  3. Reference constructors

 The meaning of this method is: Call the initialization method of the parent class;

Super (parameter): Call one of the constructors in the parent class (should be the first statement in the constructor);

This (parameter): Call the constructor of another form in this class (should be the first statement in the constructor).

  

classPerson { Public Static voidprt (String s) {System.out.println (s); } person () {PRT ("Parent class • No parameter Construction method:" + "A person."); }//construction Method (1)Person (String name) {PRT ("Parent Class • Construction method with one parameter:" + "a person's name is" +name); }//construction Method (2)}      Public classChineseextendsPerson {Chinese () {Super();//calling the Parent class construction method (1)PRT ("Subclass • Call the parent class" parameterless constructor Method ":" + "A Chinese coder."); } Chinese (String name) {Super(name);//call the parent class with the same formal parameter construction method (2)PRT ("Subclass • Call Parent Class" constructor method with one parameter ":" + "his name is" +name); } Chinese (String name,intAge ) {         This(name);//call a construction method with the same parameter (3)PRT ("Subclass: Constructor method for calling subclasses with the same formal parameters: His-age is" +Age ); }          Public Static voidMain (string[] args) {Chinese CN=NewChinese (); CN=NewChinese ("Codersai"); CN=NewChinese ("Codersai", 18); } }

Operation Result:

Parent class • No parameter construction method: A person.
Subclass • Call the parent class "parameterless construction method": A Chinese coder.
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Subclass: Calling subclasses to construct methods with the same formal parameters: His-age is 18

As you can see from this example, you can use super and this to call the constructor of the parent class and other forms of construction methods in this class, respectively.

In the example Chinese class, the third constructor method calls the second construction method in this class, and the second constructor calls the parent class, so the constructor of the parent class is called first, the second in this class is called, and the third method of construction is overridden.

Iii. similarities and differences between Super and this:

    • Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
    • This (parameter): Calls another form of the constructor in this class (should be the first statement in the constructor)
    • Super: It refers to a member in the immediate parent class of the current object (used to access member data or functions in the parent class that is 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 by name (argument)
    • This: it represents the current object name (where it is easy to generate two semantics in the program, you should use this to indicate the current object, if the function's shape participates in the class member data has the same name, this should be used to indicate the member variable name)
    • The call to super () must be written on the first line 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.
    • 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.
    • Super () and this () all need to be placed in the first row within the construction method.
    • Although you can call a constructor with this, you cannot call two.
    • 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.
    • Essentially, this is a pointer to this object, but super is a Java keyword.

Turn from: https://www.cnblogs.com/hasse/p/5023392.html, reprint please indicate the source.

Super and this usage summary in Java

Related Article

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.