Java-static, this, Super usage summary

Source: Internet
Author: User

by defining a method or member with static, it provides some convenience for our programming, which in some way can be said to be similar to global and global variables in the C language. (It's a global variable that's understood to be static) but it doesn't mean you can use it anywhere, and if that's the case, you need to think about whether you're programming with object-oriented thinking and whether your program is object-oriented.
Okay, now we're going to talk about the meaning and usage of the two keywords This&super.
in Java, this usually refers to the current object, andsuper refers to the parent class (super can inherit the method of 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, and, of course, another use of this is to invoke another constructor of the current object, which will be discussed immediately. If you want to quote something from the parent class, it's not super. Since this and the super have so many similar traits and innate relationships, we're here to discuss it, hoping to help you differentiate and master two of them.
The most common scenario in a general way is that a parameter name in your method has the same name as a member of the current object (this is the ultimate reason why this is used, avoid confusing parameters and member variables) . You will need to explicitly use the This keyword to indicate that you want to use a member using the "this. Member name", and the one without this is the formal parameter. In addition, you can use the "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 that you want to call the one. The following code demonstrates the use of the above:

1  Public classdemothis{2 PrivateString name;3 Private intAge ;4Demothis (String name,intAge ) { 5 setName (name);6 //You can add this to invoke the method, like this: This.setname (name); but that's not necessary.7 Setage (age);8  This. Print ();} 9  Public voidsetName (String name) {Ten  This. name=name;//Here you must indicate that you want to reference the member variable One }  A  Public voidEtAge (intAge ) {  -  This. age=Age ; - }  the  Public voidprint () { -System.out.println ("name=" +name+ "ge=" +Age );  - //This is not required in this line because there is nothing that will cause confusion - }  +  Public Static voidMain (string[] args) { -Demothis dt=NewDemothis ("Kevin", "22");


This code is very simple and you should be able to understand it without explaining it. In the constructor you see Using This.print (), you can completely replace it with print (), which works the same way. Here we modify this program to demonstrate the use of super.

1 classperson{2  Public intC;3 PrivateString name;4 Private intAge ;5 protected voidsetName (String name) {6  This. name=name;7 } 8 protected voidSetage (intAge ) { 9  This. age=Age ;Ten }  One protected voidprint () { ASystem.out.println ("name=" +name+ "age=" +Age );  - }  - }  the  Public classDemosuperextendsperson{ -  Public voidprint () { -System.out.println ("Demosuper:");  - Super. Print (); + }  -  Public Static voidMain (string[] args) { +Demosuper ds=NewDemosuper (); ADs.setname ("Kevin");  atDs.setage (22);  - ds.print (); - }  - }  - in Demosuper, the redefined Print method overridden The Print method of the parent class, first doing something of its own, and then invoking the overridden method of the parent class. The output illustrates this point: - Demosuper: inName=kevin age=22

The use of this method is more commonly used. In addition, if a member of the parent class can access the class, you can use it as you would with this, using the "super. Member name in the parent class" way, but often you do not access the member names in the parent class (super can also access the member variables of the parent class. However, you typically do not access members of the parent class). Constructors are a special method in constructors that are called automatically when an object is initialized. in the constructor, this and super also have various usage methods (super and this can also access the parent class's constructor, when used and access member functions, member variables a little different, not super.) but super (name), And it has a special place, see the following example:

1 classPerson {2  3      Public Static voidprt (String s) {4 System.out.println (s);5     }6  7 Person () {8PRT ("A person.");9     }Ten   One Person (String name) { APRT ("A person name is:" +name); -   -     } the } -   -  Public classChineseextendsPerson { - Chinese () { +         Super(); // calling the parent class constructor (1)  -PRT ("A Chinese.");//(4) +     } A   at 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 ) { in       This  (name); // call the constructor that currently has the same formal parameter (3)    -PRT ("His age is:" +Age ); to     } +   -      Public Static voidMain (string[] args) { theChinese CN =NewChinese (); *CN =NewChinese ("Kevin"); $CN =NewChinese ("Kevin", 22);Panax Notoginseng     } -}

In this program, this and super are no longer used as before. Connect a method or member, but follow 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 are still available in various usages of the general method, such as 4 where 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 works correctly. But it seems to be a little superfluous flavor.
Finally, write so much, if you can "this usually refers to the current object, super usually refers to the parent class" This sentence to keep in mind , then this article will achieve the goal, other you will be in the future programming practice slowly experience, master. Also on this
The inheritance mentioned in this article, see the related Java tutorials.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/yihuei123/archive/2007/06/04/1637893.aspx

Java-static, this, Super usage summary

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.