1. In the constructor, the super call is used to display the call to the parent class constructor, which is used to display the constructor that calls another overload in this class. The two can only be one and can be called at most once.
2. The assignment of a class variable is after super () or this () in the constructor.
1 Public classa{2 Private intA = 1;3 {4A = 2;5 }6 PublicA () {7A = 4;8 }9 {TenA = 3; One } A}
The execution of the above code in the JVM is:
Public class a{ privateint A; Public A () { Super(); = 1; = 2; = 3; = 4; }}
The following are examples of "crazy Java breakthroughs in 16 Lessons from programmer Fundamentals":
classbase{Private inti = 2; PublicBase () { This. display (); } Public voiddisplay () {System.out.println (i); }}classDerivedextendsbase{Private inti = 22; PublicDerived () {i= 222; } Public voiddisplay () {System.out.println (i); }} Public classTest { Public Static voidMain (string[] args) {NewDerived (); }}
The final result of the above code is 0.
In New Derived (), this object has two instance variables of I, one is the parent class base, one is the subclass Derived, and when the parent class constructor method is called, This.display () is actually a subclass method, and at this time I is not assigned a value, 0, The result of the valley output is 0.
In this case, This.display () in the base class; add a System.out.println (THIS.I) before the code; The end result is 2,0
So, what does this one represent?
When this is in the constructor, this represents the Java object being initialized. This object in the code should be derived, so why is the value of THIS.I 2? This is because <-- when a variable is compiled with a different compile-time type and run-time type, it accesses the instance variable of the object it references. The value of the instance variable is determined by the type that declares the variable. However, when an instance method of the object it references is called through the variable, the behavior of the method is determined by the object it actually refers to. -
That is, the This.i object is the type (Base) that declares the variable, while the method is called derived.
For this there is an example to look at:
1 classb{2 intCount = 2;3 Public voiddisplay () {4System.out.println ( This. Count);5 }6 }7 classDextendsb{8 intCount = 20;9 Public voiddisplay () {TenSystem.out.println ( This. Count); One } A } - Public classtest{ - the Public Static voidMain (string[] args) { -b b =NewB (); - System.out.println (B.count); - B.display (); + -D d =NewD (); + System.out.println (d.count); A D.display (); at -B BD =NewD (); - System.out.println (bd.count); - Bd.display (); - -B d2b=D; in System.out.println (d2b.count); - D2b.display (); to } +}
For B,d and simple, the output is 2, 2 20,20
For Db,bd.count, the value is 2, while Bd.display () outputs 20, the reason is bold above
For D2B, in fact, with D pointing to the same object (if the output d2b== D result is true), the value of D.count is 20, then the value of D2B is also 20? No, then the value of D2b.count is 2 (call the Display method output 20). In other words, the object that d2b points to contains two memory addresses. It also shows that Java inheritance is different when working with member variables and methods, and variables are looking at compile-time objects by looking at runtime objects.
Memory, when a program creates a subclass object, the system allocates memory not only for the instance variables defined in the class, but also for the instance variables defined in its parent class (including the indirect parent class), even if the variable names are the same (but hidden). In order to use the hidden variables defined in the parent class in a subclass method, the overridden method can use the Super keyword.
Class variables are initialized at the stage of class initialization, are classes, and can be called directly through the class. Variable name, which can also be called by the super. Variable name in the subclass (preferably using a class. Variable name).
Inheritance related knowledge