This, super usage in Java

Source: Internet
Author: User

in this section we mainly distinguish between this and super two keyword usage

At first I couldn't figure out the usage of this and Super two keywords, nor do I understand why this and super are used in code instances, and here's why:

1. In Java, this isusually referred to as the current pair , andsuper refers to the parent class .

* When you want to refer to something of the current object, such as a method of the current object, or a member of the current object, you can use this for this purpose, of course, another use of this is to invoke another constructor of the current object.

* If you want to quote something from the parent class, it's not super. Since this and super have so many characteristics and innate relationships, we must differentiate and master them two

use of 2.this keywords:

The most common scenario is that a parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to indicate that you want to use the a member, using the method of

"this. Member name", and the one without this is the formal parameter. In addition, you can use "this. Method name" to refer to a method of the current object, but this is not necessary, you can directly use the method name to access the method, the compiler will

Know what you're going to call that one. See example 3.1

Example 3.1

1 /*Defining class Domethis*/2  Public classDomethis {3      PublicString name;//declaring properties, member variables4      Public intAge ;5      PublicDomethis (String name,intAge) {//declaring Constructors6          This. name = name;//assigns the name value in the constructor to the name of the object7          This. Age = Age;//You can not add this here8          This. Print ();9     }Ten      One      Public voidPrint () {//declaring a printing method ASystem.out.println ("name" +name+ "Age" +age);//here the name and Aga can add this also can not add -     } -      the      Public Static voidMain (string[] args) { -Domethis test =NewDomethis ("Zhang", 3);  -     } -}

Operation Result:

3. Usage of the keyword Super :

1) Someone has written a good initialization property constructor, and you just want to add some of your own new properties to the initialization, so that in one constructor to call another constructor, you can avoid duplication of code, reduce the amount of work;

2) When you call another constructor in one constructor, you should use the same memory space, initialize the variable in the default constructor, and override the value of the initialized variable when calling another.

3) The entire invocation of the process and recursive call function is a bit similar, keep inflating the ball, until the whole balloon expands, continuous deep progression, encounter stop mark, layer by hop out.

See Example 3.2

1 /*define the parent class Tiger*/2  Public classTiger {3     4         intAge//Age5         intHight//Body Height6 7          PublicTiger (intAge) {//Defining Constructors8          This. Age =Age ;9 print ();Ten         } One          Public voidprint () { ASystem.out.println ("I am a" + Age + "years" + hight + "cm high tiger!"); -         } -}

The following example class Javantiger in the constructor of super () can not write, Java will default to call the parent class parameterless constructor, but if the parent class does not define a parameterless constructor, there is no syntax error, the program will automatically exit, there is no print statement, At this point you need to manually invoke the constructors of other parent classes

1 /*define subclasses see the use of Super*/2  Public classJavantigerextendsTiger {3      PublicJavantiger () {4         Super(1);//calling a constructor with a parent class with arguments5     }6      Public Static voidMain (string[] args) {7         NewJavantiger ();8     }9}

Operation Result:

The super (1) in this code must be written in, or the compiler will make an error. Simply summarize, "this () is calling its own other constructors, super () is called the constructor of its own inherited parent class," If you only want to tune

The default parameterless parent constructor is not written in the constructor of the subclass, but it is always ignored when actually programming.

So can these two keywords appear in a constructor of a subclass at the same time? The answer must be no. Let's say your own understanding:

1) When creating a new base class, regardless of the recursive invocation of its own constructor, eventually you will call the parent class constructor, (not explicitly called, the system will call the default parameterless parent constructor);

2) JAVA specifies that this and super must be placed in the first row of the constructor, with only one first row. See Figure 1

If you see where I'm wrong, please point out, thank you.

This, super usage in Java

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.