Absrtact: In Java We will often use super and this usage, now I mainly to distinguish between super. And this and Superl () and this () the difference between them to know and understand:
The usage of 1.this is in Java:
①:this.:this refers to the current object, which can manipulate all the properties and methods inside the current class and the properties and methods that the parent inherits to allow access modifiers.
Super.: Refers to the parent class object part of the current object class, cannot manipulate the properties and methods of this class, can manipulate the properties and methods that the parent class can be allowed by the parent access modifier, and only use super when the effect in this class is called before being rewritten.
Syntax: this. member name or this. Method name
Example: This is a dot used
public class Demothis {private String name;private int age;public demothis () {this.print ();//You can add this to invoke method,}public Demo This (String name, int age) {this.name = Name;this.age = age;//Here must be distinguished by the method of this dot, this.name refers to the current property name;} public void Set Name (String name) {this.name = name;//Here you must indicate that you want to reference a member variable}public void setage (int age) {this.age = age;} public void print () {System.out.println ("name=" + Name + "age=" + age);//In this line it is not necessary to use this,//because there is nothing that will cause confusion}}
Example: the use of Super dot,
Package Com.lov.lianxitext;public class Demothis {//Parent class private String name;private int age;public demothis () {}public void Print () {System.out.println ("haha ... ");}}
Package Com.lov.lianxitext;public Class Demo extends Demothis {//Subclass public demo () {}public void print () {super.print ();// Only this time with super. Method, other we use this. Method System.out.println ("Hey heh ...");}
Summary: There are above we know, this is more than super, the scope of the use, in addition to the above to see the effect before rewriting can super, we are using this.
2:this () and super ()
①this () and super () are all used only in the constructor method,
②this () calls the construct of this class, and if the visible parameter inside the parentheses is the constructor that calls the corresponding parameter of the class, super () invokes the constructor specified by the parent class,
③ can only be written in the first sentence of the construction method;
④super () can not write, the default call to the parent class when the non-argument constructs,
⑤this () and super () cannot coexist, only one can be used.
The difference between super and this in Java