The difference between this and super in Java "6"

Source: Internet
Author: User

This article original pipi-changing


Original source of this article: http://www.cnblogs.com/pipi-changing/

This document is owned by the author and the blog Park and must be retained without the author's consent.


And in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

The difference between this and super in Java

This&super

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

The Java keyword This can be used only in method body. When an object is created,

The Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer.

When you want to reference something in 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. Note that this can only be used in non-static methods in the class,

This is absolutely not possible in static methods and static code blocks. This can also be used as a constructor. You can see in the back

And what is super, which can be understood as a pointer to a super (parent) class object,

And this superclass refers to a parent class that is closest to itself. Super's role can also be used as a constructor function,

Or it gets the value of a variable of the same name for a parent object that is masked by a local variable.

Super key is similar to this function, which is a masked member variable or member method or becomes visible,

Or it is used to refer to a masked member variable and member member method.

Used as a constructor function

Super (parameter): Invokes one of the constructors in the parent class (which 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).

A few key points to keep in mind are:

In the construction method, this and super cannot coexist, and the next one where this or super appears must be the 1th sentence of the construction method;

Static methods, that is, cannot have this and Super keyword in a class method

1. Subclass constructors If you want to refer to super, you must put super in the first place of the function.

   class      Base {Base () {System.out.println ( "base" ); }}  public  class  checket extends   Base {Checket () { super (); //         Call the constructor of the parent class, and be sure to place the first statement of the method     System.out.println ("Checket" );  public  static  void   main (String argv[]) {checket c  = n    EW   Checket (); }}

Results:

Basechecket

If you want to use super to inherit the method constructed by the parent class, but not on the first line, then the statement before Super ,

It must be to satisfy the statement that you want to accomplish some behavior, but you use super to inherit the constructor of the parent class.

The changes that were made before are all back to the previous, which means that the parent class is constructed.

2. In Java, you may also encounter a member variable or method in a subclass with a superclass (sometimes called a parent class).

The member variable or method in the same name. Because a member variable or method name in a subclass has a high precedence,

So a member variable or method of the same name in a subclass hides the member variable or method of the superclass.

But if we want to use this member variable or method in the superclass, we need super.

classCountry {String name; voidvalue () {Name= "China"; }}classCityextendsCountry {String name; voidvalue () {Name= "Xian"; Super. value ();//when this method is not called, Super.name returns the value of the member variable of the parent class nullSystem.out.println (name); System.out.println (Super. Name); }     Public Static voidMain (string[] args) {City C=NewCity ();    C.value (); }}

In order to refer to the member variable in the parent class in the subclass name and method value (),

Super, Super.name, and Super.value () are used in the code, and if Super.value () is not called,

Super.name returns the default value of the parent class member variable null, when this method is called,

The Super.value () method assigns the member variable name to China, and then uses Super.name to invoke the value of the member variable of the parent class.

Also, note that the value of the member variable is called by the Super.name.

classCountry {String name= "Xian"; String value (string name) {Name= "China"; returnname; }}classCityextendsCountry {String name; String value (string name) {Name= "Shanxi"; Super. Value ("Failed");//when this method is not called, Super.name returns the value of the member variable of the parent class nullSystem.out.println (name); System.out.println (Super. Name); returnname; }     Public Static voidMain (string[] args) {City C=NewCity (); C.value (Success); }}

The result is: Shanxi

Xian

At this point, the value returned by Super.name is the value of the parent class member variable Xian, and at this point the Super.value () method is not working.

3. Pass parameters directly with super:

classPerson { Public Static voidprt (String s) {System.out.println (s); } person () {PRT ("A person."); } person (String name) {PRT (' A person name ' is: ' +name); }} Public classChineseextendsPerson {Chinese () {Super();//calling the parent class constructor (1)PRT ("A Chinese.");//(4)} Chinese (String name) {Super(name);//calling a constructor with the same formal parameters as the parent class (2)PRT ("His name is:" +name); } Chinese (String name,intAge ) {         This(name);//call the constructor that currently has the same formal parameter (3)PRT ("His age is:" +Age ); }     Public Static voidMain (string[] args) {Chinese CN=NewChinese (); CN=NewChinese ("Kevin"); CN=NewChinese ("Kevin", 22); }}

The result is:

A person.a Chinese. A person name Is:kevinhis name Is:kevina person name Is:kevinhis name Is:kevinhis:22

In this program, this and super are no longer used as before. Connect a method or member,

Instead, it follows the appropriate parameters directly thereafter, so the meaning of it changes.

Super is used to invoke constructors that have the same form in the parent class, such as 1 and 2.

This is followed by a constructor that currently has the same parameters, such as 3.

Of course, in the various overloaded constructors of Chinese, this and super can still be used for various uses in the general method, such as 4 places,

You can replace it with "this.prt" (because it inherits that method from the parent class)

or "Super.prt" (because it is a method in the parent class and can be accessed by the class), it can still run correctly.

the similarities and differences between Super and this:

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 a base class has the same member definition as a derived class, such as: Super. Variable name super. member function name (argument)

4) This: it represents the current object name (in the program is prone to ambiguity, you should use this to indicate the current object;

If the function has the same name as member data in the form of the participating class, 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 (), if the parent does not have this form of constructor,

Then you will get an error when compiling.

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,

Other constructors must also have super statements, so there are identical statements in the same constructor,

The meaning of the statement is lost, and the compiler does not pass it.

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.

Difference

This

Super

1

Action Properties

this. Property: Represents the Call Properties in this class, if

super. Property: Represents a property called in the parent class

2

action method

this. Methods (): means calling methods in this class, such as

< from the parent class Span style= "FONT-SIZE:16PX;" > super. Method (): means calling a method in the parent class

3

call construct

this () Call other constructor methods in this class

super (): Calling a constructor in a parent class by a subclass

4

Find Range

Look in the subclass first, if you don't find it from the parent class

Find the parent class directly without looking up the subclass

5

Special

Current Object

The difference between this and super in Java "6"

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.