Java notes (i) Super and this

Source: Internet
Author: User

http://zhangjunhd.blog.51cto.com/113473/20531 Summarize the keyword this and the Super usage. Author:zj 07-3-12Blog:[Url]http://zhangjunhd.blog.51cto.com/[/url] 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. Calling member variables and methods using Super&thisYou 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 {protectedvoidPrint () {System. out. println ("The Print () in class person."); }} PublicclassDemosuperextendsperson { PublicvoidPrint () {System. out. println ("The Print () in class Demosuper.");Super. print ();//Call the parent class's Method} PublicStaticvoidMain (string[] args) {Demosuper ds =NewDemosuper ();    Ds.print (); }}
 Results:The print () in class demosuper.the print () by class person. Example 2
Public class     demothis { private String name;  Public    void setName (String name) { this. Name = name;//The previous name is private name; the latter name is a parameter in SetName. }}
3. Use this to represent the object reference of the currently calling method Suppose 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 child Super (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 { 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);//The constructor that invokes the parent class with the same formal parameters.PRT("His name is:" + name); } PublicStaticvoidMain (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, which 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.

Java notes (i) Super and 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.