Briefly talk about the Super keyword in java.
The use of the Super keyword stems from the inheritance of one of the three main features of Java. In other words, with inheritance, the Super keyword is available.
Super's English basically means "super". In Java, too, super represents the meaning of super-class. And we're used to
The superclass is called the parent class.
There are two main uses of super.
One is: Call the parent class construction method
The second is: calling the parent class's properties and general methods
Here's a simple introduction to both of these usages
==================================================================== Split Line
First, the constructor method of calling the parent class
As stated above, there is the first inheritance, and then the use of super keyword. So in the code below, a simple
The inheritance of the class.
Here are two things to call the constructor of the parent class. One is to implicitly call the parent class without a parameter construction method, one is to display
Call any of the constructor methods of the parent class.
Note that the constructor method of the parent class must be called in the constructor of the subclass, not in other methods of the subclass;
Note the two point: code that calls the constructor method of the parent class must be written in the first row of the subclass construction method.
-------------------------------------------------------------------------------------------------------------Small Split Line
The first case: an implicit invocation of a parent class without a parameter construction method
The so-called implicit is that the calling process is not written out. That is, I do not write anything, the program will default to call the parent class
Non-parametric construction method. See the following code for details:
<pre name= "code" class= "Java" >public class SuperTest {public static void main (string[] args) {new Child ();//Children's class non-parametric construction New Child ("I am the child class with the parameter construction method");//Children's class with the parameter structure}}class father{string money;public Father () {//Father class non-parametric construction System.out.println (" I am the father of the non-parametric construction method "); Public Father (String t) {//Father class with parameter construction System.out.println ("I am the father of the class with the parameter construction method");}} Class children extends Father{public child () {//Kid class no parameter constructs//if the method body does not display any constructor methods that call the parent class, the default call to the parent class's empty construction method is to add a super () by default; Code System.out.println ("I am a subclass of the parameterless constructor method");} Public child (String info) {//Children class with parameter construction//If the method body does not display any constructor methods that call the parent class, the default call to the parent class's empty construction method is to add a super () by default; code SYSTEM.OUT.PRINTLN (Info );} public void Test () {}}
In the above code, three classes were written. The main class SuperTest, and a Father class, and a child class.
Where the child class inherits the Father class.
Among them, there are two construction methods in the Father class, one is non-parametric and the other is a parameter. The child class also has two constructor methods,
One is an argument, and one is a non-argument. Looking closely, you will see that the constructor method in the child class does not explicitly call the parent
The constructor method of the class. So, as I said above, the program will default to call the parent class's parameterless constructor method.
In the main class, we instantiate two child objects separately. The first call to a subclass of the parameterless construct, the second called the Child
The class has a parameter structure.
As stated above, the non-parametric construction method of the parent class is called before calling the subclass's parameterless constructor. Same
The parameterless construction method of the subclass is called before calling the subclass's parameter construction method. So the result of the above program will be:
I'm a non-parametric constructor of the parent class
I'm a subclass of the non-parametric construction method
I'm a non-parametric constructor of the parent class
I'm a sub-class with a parametric construction method
Is the result of the operation:
-------------------------------------------------------------------------------------------------------------Small Split Line
Second case: explicitly calling any of the parameter construction methods of the parent class
Since it is an explicit invocation, you must write the code clearly.
In Java, the constructor of the parent class is explicitly called in the following two ways:
Super (); //Explicitly call the parent class without a parameter construction method
Super (parameter 1, parameter 2, ...) //Show Call parent class with parameter construction method
So, the constructor in the child class above, which is an explicit invocation, results in the following:
Class Child extends Father{public children () {//Kid class no parameter constructs super ();//Explicitly call the parent class's parameterless construct, which must be written in the first row System.out.println ("I am a subclass of the parameterless constructor method");} Public child (String info) {//Children class with parameter construct super ("with parameter construction method");//Explicitly call the parent class with the parameter construct, must be written in the first row System.out.println (info);} public void Test () {}}
As stated above, the result of the modified operation will be:
I'm a father. Non-parametric construction method
I'm a subclass of the non-parametric construction method
I'm the father. Method of structure of class with parameters
I'm a child. The method of constructing the class with parameter
The result is consistent with the above, and it is not mapped.
-------------------------------------------------------------------------------------------------------------Small Split Line
The above is an introduction to the first usage of the Super keyword, which is called the parent class constructor method.
The second use of the Super keyword is to call the properties of the parent class and the general method.
==================================================================== Split Line
Two: Calling the parent class's properties and general methods
This is about calling the properties of the parent class and the general method, but why is it called? When a subclass inherits the parent class, it
Inherit its properties and methods. Now that it has been inherited, it can be used directly, why instead to call the parent class.
This involves a concept of overwrite. The so-called overwrite, refers to the subclass of the parent class to redefine the properties or methods
Once more. The name of the property is the same, the name of the method, the parameter, the return value type, the access permission (preferably the same),
Only the method body is different.
So, here's the problem.
1. When I call the Override property or method in a subclass, the property and method of the parent class is also
Is it covered in a subclass?
2, if I do not want to call the subclass of the methods and properties override, want to call the parent class in the original method and properties, then how to
Call?
Here is the answer to the question:
1, the call is the subclass of the overwrite
2, Java in the following way to invoke the properties of the parent class, or the general method:
Super. Attributes; // Call the properties of the parent class
Super. Method (); // method of calling parent class
The following is a piece of code that says:
public class SuperTest {public static void main (string[] args) {Children child = new Kid ();//Kids class without reference constructs Child.test ();}} Class father{string Money;public Father () {//Father class non-parametric construction System.out.println ("I am a father-class non-parametric construction method");} public void Test () {This.money = "1000 Yuan"; System.out.println ("I am the original method of the parent class, I have" + This.money);}} Class Child extends Father{string money;public children () {//Kid class no parameter constructs super ();//Explicit invocation of the parent class's parameterless construct must be written in the first row of System.out.println (" I am a subclass of the parameterless constructor method ");} public void Test () {This.money = "10000 yuan";//The Money attribute Super.test () of the subclass is initialized here;//Call the parent class's Test method System.out.println ("Father has" + Super.money);//Call the parent class's Money property System.out.println ("I am the test method of the subclass, I have" + This.money);//Call the child class's Money Property}}
In the above code, there is a test method in the Father class, and the child class will overwrite the test method. Then you can see that
In the child class, the test method can be passed Super.test (), and the test method of the parent class is called. You can also pass Super.money
Call the parent class's Money property.
The results of the operation are as follows:
This is the second use of the Super keyword.
==================================================================== Split Line
I was afraid that I was not clear enough, and when I looked up the information, I took some other people's words. Attached below, easy to understand. Thanks to the people online.
1.
Super key is similar to this, making a masked member variable or member method visible, or used to refer to a
Masked member variables and member member methods.
However, super is used in subclasses in order to access the masked members of the immediate parent class, which is the direct parent class (that is, the most
Near the Super Class). (Note: What is being masked here is the properties and methods that are overwritten in the subclass)
--------------------------------------------------------------------------------------------------------------- --Small split line
2.
First, in the subclass construction method to invoke the parent class constructor method, called "Super (Parameter list)" method, the parameter is not
necessary. It is also important to note that the "super (parameter list)" statement can only be used in the first row of the subclass in which the method body is constructed.
Second, when a local variable in a subclass method or a member variable of a subclass has the same name as a parent class member variable, that is, the sub-class local variable
When the amount overrides the parent class member variable, the parent class member variable is referenced with "super. Member variable name". Of course, if a member of the parent class
The variable is not overwritten, and you can refer to the parent class member variable with "super. Member variable name", but this is not necessary.
Thirdly, when a member method of a subclass overrides a member method of the parent class, that is, the child class and the parent class have exactly the same method definition (
But the method body can be different), at this time, with "super. Method name (parameter list)" Way to access the parent class method.
--------------------------------------------------------------------------------------------------------------- --Small split line
3.
Subclass constructor If you want to refer to super, you must put super in the first place of the function.
In Java, you may also encounter member variables or methods in a child class with a member variable or method in a parent class (sometimes called a parent class)
Namesake. 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 child class hides the parent
A member variable or method of a class, but if we want to use this member variable or method in the parent class, we need to use super.
--------------------------------------------------------------------------------------------------------------- --Small split line
4.
Calling the parent class's constructor method
Manipulate the member variable that is hidden and the overridden member method
--------------------------------------------------------------------------------------------------------------- --Small split line
5.
Super is used when a subclass refers to a property or method of a parent class;
Super is relative to inheritance;
Super represents the parent class of the current class, similar to this, which represents the current class.
==================================================================== Split Line
The above is a brief introduction to the Super keyword usage.
The Super keyword in Java