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. Use Super&this to call member variables and methods you can use super to access hidden variables or overridden methods of 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 1class person {protected void print () {System.out.println ("the print () of class person."); }} public class Demosuper extends person {public void print () {System.out.println ("the Print () ' in class Demosup Er. "); Super.print ();//Call the method of the parent class} public static void Main (string[] args) {demosuper ds = new Demosuper (); Ds.print (); }} Result: The print () in class demosuper.the print () in class person. Example 2public 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 indicate that the object reference of the currently calling method assumes that you want to obtain a reference to the current object within the method, using the keyword this. The This keyword can only be used inside a method, indicating that the method is calledObject "Reference. See Example 3. Example 3Button Bn;...bn.addactionlistener (this); 4. Construct a Sub Super (parameter) using the Super&this call: Call one of the constructors in the base class (which 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 4class 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 parent class constructor. PRT ("A Chinese."); } Chinese (String name) {super (name);//The constructor that invokes the parent class with the same parameters. PRT ("His name is:" + name); } public static void Main (string[] args) {Chinese cn = new Chinese (); cn = New Chinese ("Kevin"); }} Result: A person.a Chinese. A person Name Is:kevinhis name Is:kevin example 5Point (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, the difference is thatSuper calls the constructor of the parent class from the subclass, and this () invokes 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 a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will 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.
This and super