Several usages and differences of super and this in "Go" Java

Source: Internet
Author: User

Tag:ar    use    sp   java   on    data    div   bs    code    

1. Subclass constructors If you want to refer to super, you must put super in the first place of the function.  class Base { base () { system.out.println ("base");  }  }   public class Checket extends Base { checket () { super ();//Call the constructor of the parent class, be sure to put it in the first statement of the method  system.out.println ("Checket");  }   public static void Main (String argv[]) { checket C = new Checket (); } }  If you want to use super to inherit the method constructed by the parent class, but not on the first line, then the statement before super is definitely to satisfy the statement that you want to complete some behavior. But it also uses super to inherit the constructor of the parent class. The changes that were made before are all back to the previous, which means that the parent class is constructed.    2. In Java, it is sometimes encountered that a member variable or method in a subclass has the same name as a member variable or method in a superclass (sometimes called a parent class). Because a member variable or method name in a subclass has a high precedence, a member variable or method of the same name in the subclass hides the member variable or method of the superclass, but if we want to use this member variable or method in the superclass, we need to use the Super. class country {  String name;   void value () { name = ' China ';  } }   class City extends Country { string Name;   void value () { name = "Hefei";  super.value ();//When this method is not called, Super.name returns the value of the member variable of the parent class NULL SYSTEM.OUT.PRINTLN (name);  system.out.println (super.name); &nbSP;}    public static void Main (string[] args) { city c=new city ();  c.value ();  } }   In order to refer to the member variable name and method value () in the parent class in the subclass, use Super, Super.name, and Super.value () in the code, and if Super.value () is not called, Super.name returns the default value of the parent class member variable null, when this method is called, the Super.value () method assigns the member variable name to China, and then uses Super.name to invoke the value of the member variable of the parent class.   Also note that Super.name calls the value of the member variable,  class country { string name= "Xianfan";    string value (String name) { name = "China",  return Name; } }   class City extends country { string name;    string value (String name) { name = "Hefei";  super.value ("failed");//When this method is not called, Super.name returns the value of the member variable of the parent class NULL SYSTEM.OUT.PRINTLN (name);  system.out.println (super.name);  return Name; }   public static void Main (string[] args) { city c=new city ();  c.value ("Success");  } }  result is:hefei xianfan  at this point, the value returned by Super.name is Xianfan the value of the parent class member variable, and the Super.value () method at this time is not working.    3. With SuPer direct pass parameter:  class person { public static void prt (String s) { system.out.println (s); }    person () { prt ("A person.");  }   person (String name) { prt ("A person name is:" + name); } }    Public class Chinese extends person { chinese () { super ();//Call the parent class constructor (1)  prt ("A Chinese."); /(4)  }   chinese (String name) { super (name);//The constructor that invokes the parent class with the same parameter (2)  prt ("His name is:" + Name),  }   chinese (String name, int age) { this (name);//Call the constructor that currently has the same formal parameter (3)  prt ("his Age was: "+ age";  }   public static void Main (string[] args) { chinese cn = new Chinese ();  CN = new Chinese ("Kevin");  CN = new Chinese ("Kevin"); } }  Result: A person. a chinese. a Person name Is:kevin his name is:kevin a person name Is:kevin his name Is:kevin his age is:22  in this section of the 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.    4. Similarities and differences between Super and this:  1) Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)  2) This (argument): Call another constructor in this class (which should be the first statement in the constructor)  3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member functions by name (argument)  4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, if the function's shape participates in the class member data with the same name, then need this to indicate the member variable name)    5) call Super () The first line of the subclass construction method must be written, 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.   6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () calls other methods within the same class.   7) Super () and this () need to be placed in the first line of the construction method.   8) Although you can call a constructor with this, you cannot call two.   9) 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.  ) This () and super () all refer to objects, so they cannot be used in a static environment. Includes: STATic variable, static method, static statement block.   11) In essence, this is a pointer to this object, but super is a Java keyword.

There are several uses and differences between super and this in

Go Java

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.