Java programming-this and super

Source: Internet
Author: User

Java programming-this and super

8.8 this and super

The following describes how to use the this and super keywords. By using the this and super keywords in a program, you can easily reference a lot of content inside the class and understand the implementation principle of object-oriented, it is easier to understand the internal implementation of object-oriented technology.

8.8.1 this keyword

This keyword represents itself. The main application in the program has the following aspects:

L use the this keyword to reference member variables

L use the this keyword to reference other constructor methods within the constructor.

L use the this keyword to represent the object of its own class

L use the this keyword to reference the member Method

8.8.1.1 reference member variables

In a class method or constructor, you can use the format "this. member variable name" to reference the member variable name. In some cases, it can be omitted. In some cases, it cannot be omitted. First, let's look at the following code:

/**

* Use this to reference member variables.

*/

Public class ReferenceVariable {

Private int;

Public ReferenceVariable (int ){

This. a =;

}

Public int getA (){

Return;

}

Public void setA (int ){

This. a =;

}

}

Both the constructor method of the Code and the setA method use this. a to reference the member variables of the class. Because both the constructor method and the setA method contain two variables named a. One is parameter a and the other is member variable. According to the scope of variables in the Java language, the function scope of parameter a is inside the constructor or method, and the function scope of member variable a is inside the class, in this way, there is a conflict between the constructor method and the setA method. the Java language specifies that when the scope of a variable overlaps, a variable with a small scope will overwrite a variable with a large scope. Therefore, in the constructor and setA methods, parameter a takes effect.

To access member variable a, you must use this for reference. Of course, if the variable names do not overlap, this can be omitted.

However, to enhance the readability of the Code, the parameter name is generally consistent with the name of the member variable, so this is often used within the standard code.

8.8.1.2 reference Constructor

Within the constructor of a class, you can also use the this keyword to reference other constructor methods. this can reduce code duplication and ensure that all constructor methods are unified, this facilitates code modification and maintenance in the future, and also facilitates code reading.

The following is a simple example:

/**

* Use the this keyword to reference the constructor.

*/

Public class ReferenceConstructor {

Int;

Public ReferenceConstructor (){

This (0 );

}

Public ReferenceConstructor (int ){

This. a =;

}

}

Here, inside the constructor without parameters, this is used to call another constructor. 0 is the value of the parameter to be passed. When there are many constructor methods in a class, only the internal function code of one constructor can be written. Other constructor methods are called to implement this constructor. This ensures that all constructor structures are unified, it also reduces code duplication.

In actual use, note that when the this keyword is used inside the constructor to call other constructor methods, the called code can only appear in the first line of executable code within the constructor. In this way, calling the constructor using the this keyword within the constructor can appear at most once.

8.8.1.3 represents the object

Inside a class, you can also use this to represent the objects of its own class, or in other words, each class has an implicit member variable. The type of this member variable is the type of this class, the name of this member variable is this. The sample code of the object that actually uses this to represent its own class is as follows:

/**

* Use this to represent the objects of the class.

*/

Public class ReferenceObject {

ReferenceObject instance;

Public ReferenceObject (){

Instance = this;

}

Public void test (){

System. out. println (this );

}

}

In the constructor, assign the value of this to the instance. In the test method, the content of this object is output. this indicates the object of its own type.

8.8.1.4 reference the member Method

You can also use "this. the method name (parameter) is used for reference, but this can be omitted in all such references, so this is not detailed here.

8.8.2 super keyword

The Chinese meaning of the super keyword is super. You can use the super keyword to reference the content in the parent class in the subclass. There are several main forms of use:

L reference the constructor of the parent class in the constructor of the subclass.

L call the member methods in the parent class in the subclass

L call the member variables in the parent class in the subclass

8.8.2.1 reference the parent class Constructor

When constructing a subclass object, you must call the constructor of the parent class. To facilitate code writing, the default constructor in the parent class is automatically called within the constructor of the subclass. However, if the parent class does not have a default constructor, you must call it manually.

Using super, you can call the constructor of the parent class within the constructor of the subclass. You can call the constructor in the parent class as needed within the constructor of the subclass.

The sample code for calling the parent class constructor using the super keyword is as follows:

// File name: SuperClass. java

Public class SuperClass {

Public SuperClass (){}

Public SuperClass (int ){}

}

// File name: SubClass. java

Public class SubClass extends SuperClass {

Public SubClass (){

Super (); // can be omitted

}

Public SubClass (int ){

Super ();

}

Public SubClass (String s ){

Super (); // can be omitted

}

}

In this sample code, SubClass inherits the SuperClass class. Within the SubClass constructor, you can use the super keyword to call the constructor of the parent class. There is no limit on which constructor to call, it can be called within the subclass as needed, but the appropriate parameters can be passed in according to the calling constructor.

The SuperClass of the SubClass class has a default constructor, so the super () code in the constructor can be omitted.

Like calling a constructor using the this keyword, the code of super calling constructor can only appear in the first line of executable code in the subclass constructor. In this way, the super code that calls the constructor appears within the constructor of the subclass, and cannot be used together with the code that calls the constructor of this.

8.8.2.2 reference the parent class member Method

The Child class inherits the member methods of the parent class, which can be directly used by the method name. However, if the child class overwrites the member methods of the parent class, if you need to call the override member method in the parent class within the subclass, you cannot directly call it. In this way, you need to use the super keyword again.

The sample code is as follows:

// File name: SuperClass2.java

Public class SuperClass2 {

Public void test (){}

Public void print (int ){

System. out. println ("SuperClass2:" + );

}

}

// File name: SubClass2

Public class SubClass2 extends SuperClass2 {

Public void print (int ){

Super. print ();

System. out. println ("SubClass2 ");

}

Public void t (){

Super. test (); // super can be omitted

Super. print (0); // cannot be omitted

}

}

8.8.2.3 reference a parent class member variable

If the member variable of the parent class is referenced in the subclass, you can also use "super. "member variables" are referenced, but it is meaningless to overwrite the member variables. Therefore, you can directly use the member variable name for reference at this time, so super here can be omitted.

8.8.3 notes

Finally, in actual use of this and super, in addition to the issues that need to be noted above, it is also worth special attention that this and super are non-static, therefore, none of these two keywords can be used within the static method.


 

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.