Use of super and this

Source: Internet
Author: User

Source: http://zhangjunhd.blog.51cto.com/113473/20531/ 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. call the constructor using super&this classperson { Public Static voidPRT (String s) {System. out. println (s); } person () { PRT("A person."); } person (String name) { PRT("A person name is:" + name); }} Public classChinese extendsperson {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); } Public Static voidMain (string[] args) {Chinese CN = NewChinese (); CN = NewChinese ("Kevin"); }}Results:A Person.a Chinese. A Person Name Is:kevin 3 . 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. His name Is:kevin

Super and this use

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.