Java keyword--super, this

Source: Internet
Author: User
Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held. http://zhangjunhd.blog.51cto.com/113473/20531 summarizes the keyword this and super usage. AUTHOR:ZJ 07-3-12 Blog: [Url]http://zhangjunhd.blog.51cto.com/[/url]
1. What is super. What is this . The Super keyword represents the meaning of the super (parent) class. The This variable represents the object itself.
2. Using Super&this calling member variables and methodsYou can use super to access a variable or overridden method that is hidden by the parent class quilt class. The current class is inherited from a superclass when the super is called. XX () is the XX () method that invokes the base class version. See Example 1. When a class has two variables of the same name, one belongs to the class (the member variable of the class), and the other belongs to a particular method (a local variable in the method), use this to differentiate between member and local variables.   See Example 2. Example 1
classperson {protectedvoidPrint () {System. Out. println ("The Print () in class person."); } } PublicclassDemosuperextendsperson { PublicvoidPrint () {System. Out. println ("The Print () in class Demosuper.");Super. print (); Calling the parent class method} PublicStaticvoidMain (string[] args) {Demosuper ds =NewDemosuper ();     Ds.print (); } }
Result: the print () in class Demosuper.   The print () in class person. Example 2
Public  class        demothis { private String name;   Public void setname (String name) { this. Name = name;//previous name is private name; the latter name is Setnam     The parameters in E. } }

3. Use this object reference that represents the current calling methodIf you want to get a reference to the current object within the method, use the keyword this. The This keyword can only be used within a method, representing a reference to the object that invokes the method.   See Example 3. Example 3
Button bn; ... bn.addactionlistener (this) ;

4. To call a constructor using Super&thisSuper (parameter): Calls one of the constructors in the base class (should be the first statement in the constructor). See Example 4. This (argument): Calls another constructor that is formed in this class (should be the first statement in the constructor).   See Example 5. Example 4
classperson { PublicStaticvoidPRT (String s) {System. Out. println (s);     Person () {prt ("A person.");     Person (String name) {prt ("A person name is:" + name); } } PublicclassChineseextendsperson {Chinese () {Super();         Call the parent class constructor.     PRT ("A Chinese."); } Chinese (String name) {Super(name);         Invokes a constructor with the same formal parameter as the parent class.     PRT ("His name is:" + name); } PublicStaticvoidMain (string[] args) {Chinese CN =NewChinese (); CN =NewChinese ("Kevin"); } }
Result: A person. A Chinese. A person name Is:kevin His name Is:kevin example 5
Point ( int  a, int  b) {   &N Bsp;x=a;     y=b; Point () {     this

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.