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 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 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);
}
Public Static void Main (string[] args) {
City c=New city ();
C.value ();
}
}
In order to refer to the member variables in the parent class in the subclass name and method value (), Super, Super.name, and Super.value () are used in the code, and if Super.value () is not called, the 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 the value of the member variable is called by the Super.name.
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");
}
}
The result is: Hefei
Xianfan
At this point, the value returned by Super.name is the value of the parent class member variable Xianfan, and at this point the Super.value () method is not working.
3. Pass parameters directly with super:
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 (); Calling 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", 22);
}
}
The result is: 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 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. The 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 (parameter): Invokes another form of the constructor in this class (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 function 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, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)
5) 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.
6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
7) Super () and this () must 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 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.
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.
Several uses of super in Java and differs from this