Java keyword Introduction of this and super

Source: Internet
Author: User

1. What is super? What is this ? the Super keyword denotes 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, if inherited from a superclass, calls Super. XX () is the XX () method that calls the base class version. See Example 1. when there are two variables of the same name in the class, one belongs to the class (the member variable of the class), and the other belongs to a specific method (local variable in the method), use this to differentiate the member variable and the local variable. See Example 2.   Example 1
classPerson {    protectedvoid print () {       System. out. println ("the print () in class person.");    }}Public class DemosuperextendsPerson {    Public void print () {       System. out. println ("the print () in class Demosuper.");       Super. Print ();//calling a method of the parent class    }    Public staticvoid Main (string[] args) {       Demosuper ds =NewDemosuper ();       Ds.print ();    }}
  Results:The print () in class Demosuper.The print () in class person.  Example 2
Public class Demothis {    PrivateStringname;    Public void setName (String name) {       This.name= name;//previous onenameis aPrivate Name; the latter onenameis aSetNamethe parameters in the.     }}
3. Use this An object reference that represents the current calling methodSuppose you want to get a reference to the current object within the method, using the keyword this. The This keyword can only be used inside a method, representing a reference to the "object that called the method". See Example 3.   Example 3
Button Bn ; ... Bn.addactionlistener (this);
4. Using Super&this to invoke the constructor childSuper (parameter): Call one of the constructors in the base class (should be the first statement in the constructor). See Example 4. this (parameter): Invokes another form of the constructor in this class (which should be the first statement in the constructor). See Example 5.   Example 4
classPerson {    Public staticvoid prt (String s) {       System. out. println (s);    }    Person () {       PRT("A person.");    }    Person (String name) {       PRT(' A person name ' is: '+ name);    }}Public class ChineseextendsPerson {    Chinese () {       Super();//Call the parent class constructor.        PRT("A Chinese.");    }    Chinese (String name) {       Super(name);//call the constructor of the parent class that has the same formal parameters.        PRT("His name is:"+ name);    }    Public staticvoid Main (string[] args) {       Chinese CN =NewChinese ();       CN =NewChinese ("Kevin");    }}
  Results:A person.A Chinese.A person name Is:kevinHis name Is:kevin  Example 5
point ( int A, int b) {     x=a;     y=b; } point () {      this // call point, must be the first statement. }
5. What should I pay attention to when using super&this? 1) 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 (), and if the parent does not have this form of constructor, it will be error-free at compile time.  2) Super () and this () are similar, except that super calls the constructor of the parent class from the subclass, this () calls other methods within the same class.  3) Super () and this () must be placed in the first line of the construction method.  4) Although you can call a constructor with this, you cannot call two.  5) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have the super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, The compiler also does not pass.  6) This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.  7) In essence, this is a pointer to this object, but super is a Java keyword. 6 . References[1]thinking in Java 3rd[2] This super is used in Java should be noted in the place[Url]http://hengdao325200.blog.163.com/blog/static/340522006626114027/[/url][3] The correct usage of this and super in the Java class body[Url]http://xiaocao000.spaces.live.com/blog/cns!f[/url]826a925cf33491a!126.entry 7. Related introduction
Introduction to the Java keyword Static Introduction to the Java keyword Final

Java keyword Introduction of this and super

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.