Chapter 6 Repeats Classes
One. Inheritance
1. When overriding a function in the Derived Class, you can only overwrite the interface in the base class, that is, the public or protected or friends function in the base class. If you try to overriding a private function, although it is compiled, it actually just adds a function to the DeriveD Class. Such as
Class cleanser {
Private void PRT () {// (b)
System.out.println ("Cleanser.PRT ()");
}
}
Public class explicitstatic extends cleanser {
Public void prt () {
System.out.println ("ExplicitStatic.PRT ()");
}
Public static void main (String [] args) {
Cleanser x = new explicitstatic ();
X.PRT (); // (a)
}
}
Because PRT () in Cleanser is Private, it cannot be overwritten in its DeriveD Class. The PRT () in ExPlicitStatic is just a function in the ExPlicitStatic, so when trying to call the PRT () by polymorphism at (a), an error occurs. If the private at (b) is removed, the result is
ExplicitStatic.PRT ()
2. SUPER's use
1) You can call the current Class SuperClass (parent class) through the keyword super.
Example 6.1.1.1
Class base {
Base () {system.out.println ("base ()");}
Public void scrub () {system.out.println ("Base.scrub ()");}
}
Class Cleanser Extends Base {
Private string s = new string ("cleanser");
Public void append (string a) {s = a;
Public void dilute () {append ("dilute ()");}
Public void apply () {append ("apply ()");}
Public void scrub () {append ("scrub ()");}
Public void print () {system.out.println (s);}
Cleanser () {
System.out.println ("Cleanser ():" S);
}
Public static void testStatic () {
System.out.println ("TestStatic ()");
}
Public static void main (String [] args) {
Cleanser x = new cleanser ();
X.Apply (); x.scrub (); x.print ();
}
}
Public class explicitstatic extends cleanser {
ExplicitStatic () {
System.out.println ("ExplicitStatic ()");
}
Public void scrub () {
"" detergen.scrub () ");
Super.TestStatic ();
Super.scrub (); // Call Cleanser.SCRUB ()
}
Public void foam () {append ("foam ()");}
Public static void main (String [] args) {
ExplicitStatic X = New ExplicitStatic ();
X.Apply (); x.scrub (); x.foam ();
X.Print (); System.out.println ("Test Base Class:");
Cleanser.main (args);
TestStatic ();
}
}
operation result:
Base ()
Cleanser (): Cleanser
ExplicitStatic ()
TestStatic ()
Cleanser dilute () apply () detergen.scrub () Scrub () Foam ()
Test base Class:
Base ()
Cleanser (): Cleanser
Cleanser dilute () Apply () Scrub ()
TestStatic ()
2) When you call a member in SuperClass by SuperClass, the nearest member is called.
Example 6.1.1.2
Class base {
protected string bases = "base"; // (a)
// private string bases = "base";
Base () {system.out.println ("base ()");}
}
Class Cleanser Extends Base {
protected string bases = "cleanser"; // (b)
Public string s = new string ("cleanser");
Cleanser () {
System.out.println ("Cleanser ():" S);
}
Cleanser (String a) {
System.out.println ("Cleanser (" A "): s =" s);
}
}
Public class explicitstatic extends cleanser {
String s2 = s;
String Bases = super.bases; // (c)
ExplicitStatic () {
Super ("ExplicitStatic");
System.out.println ("ExplicitStatic (): S2 =" S2 ", BASES =" Bases "Super.bases =" Super.bases);
Bases = "explicitstatic";
System.out.println ("Bases =" Bases ", Super.bases =" Super.bases);
Public static void main (String [] args) {
ExplicitStatic X = New ExplicitStatic ();
}
}
Result 1:
Base ()
Cleanser (ExplicitStatic): s = Cleanser
ExplicitStatic (): S2 = CLEANSER, BASES = Cleanser, Super.bases = Cleanser
Bases = expectedstatic, super.bases = cleanser
In the above example, there is a String Bases instance in three classes. If BASES is called directly in ExPlicitStatic, the actual call is the BASES (ie (c) member) in the current class EXPLICITATIC; if Bases is called through super.bases, the nearest Bases member is closest to the current class explicitstatic. The Bases instance (ie (b)) in the Cleanser Class is as shown in the result 1. If the (b) is commented out, Bases in the base class will be called, and the result is shown in the result 2.
Result 2:
Base ()
Cleanser (ExplicitStatic): s = Cleanser
ExplicitStatic (): S2 = Cleanser, Bases = Base, Super.bases = BASE
Bases = expected attemtatic, super.bases = base
3. Initialization of Base Class
2.1 When you produce a Derived Class object, it will contain the Base Class sub-object (SubObject). This sub-object is exactly the same as you separately produce the Base Class object.
2.2 The constructor of the base class can be called by super (), but must be placed in the first line of the constructor and can only be applied in the constructor.
2.3 The order of initialization is:
1) Load code (.CLASS file)
2) Initialize the static member of the class, initialize the "from the inner", that is, starting from the base class.
3) Call the constructor of the base class in the constructor of the DeriveD Class.
If you do not call the BASE Class constructor in the DeriveD Class constructor, the compiler calls the BASS Class's default constructor and automatically generates the corresponding call statement to generate an Base Class instance. If the constructor of the parent class is called by the super () display in the constructor of the DeriveD Class, the specified constructor is called. The call sequence of calling the constructor is "from the outside".
4) Call the constructor of Derived Class.
**: When the base class does not have a DEFAULT constructor, the constructor of the Base Class must be invoked through the Super Class constructor in the constructor of the Derive Class. Example: The initialization process of the following code is:
1) Load the code of ExplicitStatic (load the explicitStatic.class file).
2) Discover the code of the Base Class, which is the Base Class of ExplicitStatic, loads the Cleanser.class file.
3) Find the Cleanser with keyword Extends, load the Cleanser's base class code (load base.class file).
4) Initialize static members in Base Class.
5) Initialize static members in the Cleanser Class.
6) Initialize static members in the ExplicitStatic Class.
If you comment on the code at (c), then the initialization work is over.
7) Assign the storage space for the ExplicitStatic object and initialize the storage space to 0.
8) Call the SUPER ("ExplicitStatic") in the configuration of the ExplicitStatic Class (Explicitly call the constructor of the parent class in the constructor of the EXPLICITATIC Class), trying to generate a Cleanser Class instance.
9) Allocate storage space for the Cleanser object and initialize the storage space to 0.
10) Because the Cleanser Class is inherited from the Base Class, it will pass Super () in the constructor of the Cleanser Class (since the constructor of the parent class is not explicitly invoked, the DEFAULT constructor of the parent class is called the parent class. Constructor is trying to generate a Cleanser Class instance.
11) Generate an Base Class instance. First initialize the member variable, then call the constructor.
12) Go back to the Cleanser Class to generate an example. First initialize the member data in the Cleanser Class, and then perform the rest of the constructor Cleanser (String A).
13) Back to the ExplicitStatic Class, generate an instance. First, the member data in the ExplicitStatic Class is initialized, and then the rest of the constructor explicitStatic ("system.out.println (" ExplicitStatic () ")).
Class base {
Static Int S1 = PRT ("S1 Initialized.", 11);
INT I1 = PRT ("I1 INITIALIZED.", 12);
Base () {
System.out.println ("Base ()");
System.out.println ("S1 = S1 ", I1 = " I1);
DRAW (); // (d)
}
Void Draw () {
System.out.println ("BASE.DRAW: S1 =" S1 ", I1 =" I1);
Static Int PRT (String S, INT NUM) {
System.out.println (s);
Return Num;
}
}
Class Cleanser Extends Base {
Static Int S2 = PRT ("S2 Initialized.", 21);
INT I2 = PRT ("I2 Initialized.", 22);
Cleanser () {
System.out.println ("Cleanser ()");
System.out.println ("S2 =" S2 ", I2 =" I2);
}
Cleanser (String a) {
// super (); (b)
System.out.println ("Cleanser (" A ")")
System.out.println ("S2 =" S2 ", I2 =" I2);
}
Void Draw () {
System.out.println ("Cleanser.draw: S2 =" S2 ", I2 =" I2);
}
}
Public class explicitstatic extends cleanser {
Static Int S3 = PRT ("S3 Initialized.", 31);
INT i3 = PRT ("I3 Initialized", 31);
ExplicitStatic () {
Super ("ExplicitStatic"); // (a)
System.out.println ("ExplicitStatic ()");
System.out.println ("S3 =" S3 ", I3 =" I3);
}
Public static void main (String [] args) {
ExplicitStatic x = new explicitStatic (); // (c)
}
}
result:
S1 INITIALIZED.
S2 Initialized.
S3 Initialized.
/ / If the code at (c) is commented away, the output result is not to be output without outputting the result.
I1 Initialized.
Base ()
S1 = 11, i1 = 12
Cleanser.draw: S2 = 21, I2 = 0 // (D)
I2 Initialized.
Cleanser (ExplicitStatic) / / (a)
S2 = 21, i2 = 22
i3 Initialized
ExplicitStatic ()
S3 = 31, i3 = 31
Since I2 in the Cleanser has not initialized in base (), when the storage space is allocated to the Cleanser object, the storage space is initialized to 0, so I2 is 0.2.4 code and the result. (A) illustrates an instance of the base class of the current Class, otherwise the member of the base class cannot be called in the DeriveD Class. When calling the Cleanser Class constructor Cleanser (String A), in Cleanser (String A) is not explicitly invoked with the Base Class constructor, the system automatically generates the statement of the Default constructor of the Base Class, such as ( b).
4. Snatch between combination and inheritance
1) Inheriting is a "IS-A (one)" relationship, such as the truck is one of the car; the combination is a "HAS-A (with one)" relationship, such as a car There are four wheels.
2) Do you need to transform the new Class up to Base Class.
5. Access permission in inheritance
The protect variable can be called by the children. If BASES in Base Class can be called by Cleanser Class and ExplicitStatic Class.
Class base {
protected string bases = "base";
// private string bases = "base";
Base () {system.out.println ("base ()");}
}
Class Cleanser Extends Base {
protected string bases = "cleanser";
Public string s = new string ("cleanser");
Cleanser () {
System.out.println ("Cleanser ():" S);
}
Cleanser (String a) {
System.out.println ("Cleanser (" A "): s =" s);
}
}
Public class explicitstatic extends cleanser {
String s2 = s;
String bases = super.bases;
ExplicitStatic () {
Super ("ExplicitStatic");
System.out.println ("ExplicitStatic (): S2 =" S2 ", BASES =" Bases "Super.bases =" Super.bases);
Bases = "explicitstatic";
System.out.println ("Bases =" Bases ", Super.bases =" Super.bases);
}
Public static void main (String [] args) {
ExplicitStatic X = New ExplicitStatic ();
}
}
result:
Base ()
Cleanser (ExplicitStatic): s = cleanserexplicitStatic (): S2 = Cleanser, Bases = Cleanser, Super.bases = Cleanser
Bases = expectedstatic, super.bases = cleanser
two. Keyword Final
1. Final Data
1.1 Final Data
1) When the basic type is defined as final, indicating that its data value cannot be changed. Such as
Final INT I = 9;
i ; // Compiling errors, can not change the value of i
2) When the Object Reference is defined as FINAL, it cannot be changed, but only Reference is instead of the object itself. Such as
Class value {
INT i = 1;
}
Public class explicitstatic extends cleanser {
Public static void main (String [] args) {
Final Value v = new value (); // v.i = 1
V.I ; // v.i = 2
// v = new value ();
}
}
Since V is Final, you cannot re-point to an object via new value; however, the value of the object points to V.i ) can be changed (V.I ).
1.2 Blank Finals
We can declare the data member as Final but do not give initial values, this is Blank Finals. But Blank Finals must only be initialized in the constructor.
PUBLIC CLASS ExplicitStatic {
Final INT IB;
Final INT I = 1;
ExplicitStatic ()
{
IB = 2; // (a)
// i = 3; (b)
System.out.println ("i =" i ", IB =" ib);
}
Public static void main (String [] args) {
ExplicitStatic EX = New ExplicitStatic ();
}
}
IB is Blank Final, so it can be initialized in a constructor. If the code at (a) is commented away, IB does not have initial values, compiling errors. And I has been initialized at the definition, and the value of the value of i is not changed, and the code compile error at (b) is not changed.
**: Non-Blank Finals member cannot change its value even in the constructor
2. Final Methods
1) Function that is declared for final cannot be overwritten
2) All private functions in the class will naturally be final.
1. Final Classes
1) When a class is declared as FINAL, it means that it cannot be inherited, but the Class's data member is not final, which can be changed. Such as
Class smallbrain {}
Final class dinosaur {
INT i = 7;
INT j = i;
Smallbrain x = new smallbrain ();
Void f () {};
}
/ / Cannot inherit Final function
// Class Further Extends Dinosaur {} public class explicitstatic {
Public static void main (String [] args) {
Dinosaur n = new dinosaur ();
n.f ();
n.i = 40; // Final Class Non-final data member can be changed
N.j ;
}
}
2) All functions in the final class are naturally final because no one can overwrite.