Polymorphism (above) basically explains many polymorphic characteristics and problems. Continue below.
1) Constructors and polymorphic
This question is actually written before, the constructor is actually a static method, but is implicitly declared, so the constructor is not polymorphic.
But you need to know the order of loading.
Class grandfather{ grandfather () { print (); } private int print () { System.out.println ("G"); return 1;} } Class Father extends grandfather{ Father () { print (); } private int print () { System.out.println ("F"); return 1;} } public class son extends father{ son () { print (); } Private int print () { System.out.println ("s"); return 1; } public static void Main (string[] args) { son s = new Son ();} }
in fact, when new out of the subclass, you need to call the parent class constructor, recursively go down.
So the output is g,f,s.
2) Inheritance and cleanup
Although there is a GC, the book uses a technique of reference to simulate cleanup objects.
Class count{private int reference; private static int counter; Private final Long id = counter++;//Note that, although it is final, it may be thought that it is not immutable//Why give him a value, right, but now the value is not sure, we will not change after assigning him a value. Count () {System.out.println ("Count" +id); } public void AddReference () {reference + +; } protected void Dispose () {if (--reference = = 0) {System.err.println ("Dispose count" +id); }}}public class Rubbish {private count count; private static int counter; Private final Long id = counter++; Rubbish (count count) {System.out.println ("rubbish" +id); This.count = count; This.count.addReference (); } protected void Dispose () {System.out.println ("dispose rubbish" +id); Count.dispose (); } public static void Main (string[] args) {count count = new Count (); Rubbish rubbish[] = {new Rubbish (count), new rubbish (count), new rubbish (count), new rubbish (count)}; for (rubbish r:rubbish) {r.dispose (); } }}
For each new object, the counter counter calculates the number of Count objects, the ID is final we do not want to be changed after, reference is the reference count, each object adds one, will add one, when the reference is not, We also want to clean up the object referenced by the calculation.
The reference counting method in the original GC is one such principle.
3) Design with inheritance
TV Big Body Change:
Class tv{ public String getString () { return "TV"; } Public TV Change () { //TODO auto-generated method stub return new TV ();} } Class Santv extends tv{ public String getString () { return "Santv"; }} public class LeTV extends tv{public String getString () { return "LeTV"; } Public Santv Change () { return new Santv (); } public static void Main (string[] args) { TV LeTV = new LeTV (); System.out.println (Letv.getstring ()); TV newtv = Letv.change (); System.out.println (Newtv.getstring ());} }
before making a mistake, there is no change method in the TV class, I used it directly
TV newtv = Letv.change ();
found error, TV does not define the change method, my sub-class can have their own new method, why the error?
After the understanding, the parent class refers to the subclass object, in fact, the first call is the parent class method, due to the existence of polymorphism, late binding, will be combined with the specific override method. But I now have no definition of the parent method, definitely error. In order to verify, modify the code.
public class LeTV extends tv{public String getString () { return "LeTV"; } public static void Main (string[] args) { TV LeTV = new LeTV (); System.out.println (Letv.getstring ()); TV newtv = Letv.change (); System.out.println (Newtv.getstring ());} }
now that the subclass does not override the Change method, the default is to inherit the parent class's change method, so the output is LETV,TV.
In fact, the above conversion is a special mode-state mode.
In fact, TV can be seen as a list, two specific TVs can be regarded as LinkedList and ArrayList. The two states can be flexibly switched.
Polymorphism is here--in different forms.
To really understand, actually need to knock on the code, after writing, you will find that you are using polymorphic.
Java programming Thought (v)-polymorphism (bottom)