Class? Base? {
Base()? {
system . out . println(" Base ");
}
}
Public? Class? Checket? Extension? Base? {
Checket()? {
super(); //Calling the constructor of the parent class must be placed in the first statement of the method.
system . out . println(" check et ");
}
Public? Static electricity Invalid? Main (string? argv[])? {
Checket? c? =? New? checket();
}
} If you want to use super to inherit the method constructed by the parent class, but it is not in the first line, then the statement before super must be to satisfy the statement that you want to complete a certain behavior, but you inherit the parent class by using the method constructed by super. Then all the changes made before will go back to the past, that is, it will become the constructor of the parent class again.
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 priority, a member variable or method with the same name in a subclass hides a member variable or method of a superclass, but if we want to use this member variable or method in a superclass, we need to use super.
Class? Country? {
String? Name;
Invalid? value()? {
Name? =? "China";
}
}
Class? City? Extension? Country? {
String? Name;
Invalid? value()? {
Name? =? Hefei;
super . value(); //When this method is not called, super.name returns the value of the parent class member variable null.
System.out.println (name);
system . out . println(super . name);
}
Public? Static electricity Invalid? 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, super, super.name and super.value () are used in the code. If super.value () is not called, super.name will return the default value of the member variable of the parent class to null. When calling this method, the super.value () method assigns the member variable name to China, and then uses super.name to call the member variable value of the parent class.
In addition, it should be noted 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? Extension? Country? {
String? Name;
String? Value (string? Name)? {
Name? =? Hefei;
Super.value ("failure"); //When this method is not called, super.name returns the value of the parent class member variable null.
System.out.println (name);
system . out . println(super . name);
Return? Name;
}
Public? Static electricity Invalid? main(String[]? args)? {
City? C = new? city();
C.value ("success");
}
The result is: Hefei
Xianfan
At this time, the value returned by super.name is the value of the parent class member variable xianfan, and the super.value () method does not work at this time.
3. Directly pass parameters with super:
Class? People? {
Public? Static electricity Invalid? Prt (string? s)? {
system . out . println(s);
}
Person()? {
prt("A? People. ”);
}
Person (string? Name)? {
prt("A? People? Name? Yes: "? +? Name);
}
}
Public? Class? China? Extension? People? {
Chinese ()? {
super(); ? //? Call the parent class constructor (1)
prt("A? China people. ”); //? (4)
}
Chinese (string? Name)? {
Super (name); //? Call the constructor (2) whose parent class has the same formal parameters.
Prt ("His? Name? Yes: "? +? Name);
}
Chinese (string? Name,? int? Age)? {
This (name); //? Call the constructor (3) that currently has the same formal parameters.
Prt ("His? Age? Yes: "? +? Age);
}
Public? Static electricity Invalid? main(String[]? args)? {
China? cn? =? New? Chinese ();
cn? =? New? Chinese ("Kevin");
cn? =? New? Chinese ("Kevin"? 22);
}
The result is: one person.
A man from China.
A person's name is: Kevin
His name is Kevin.
A person's name is: Kevin
His name is Kevin.
His age is: 22.
In this program, this and super do not use "."to connect methods or members as before, but directly follow the appropriate parameters, so its meaning has changed. The parameters after super are used to call constructors of the same form in the parent class, such as 1 and 2. After that, add parameters to call the constructor with the same parameters at present, such as 3 bits. Of course, in Chinese overloaded constructors, various usages of this and super in general methods can still be used. For example, in four places, you can use "this.prt" (because it inherits the method in the parent class) or "super.prt" (because it is a method in the parent class and can be accessed by subclasses) and still run correctly. But this seems a bit superfluous.