In the previous chapter, the Java Programming Idea (iv)--The reuse class speaks of upward transformation, and feeling and polymorphism are better written together.
Polymorphic, polymorphism. An important feature, the length of the space is too long, divided into two articles written.
(1) Upward transformation
Class tv{public static void Show (TV TV) { System.out.println ("TV");} } public class LeTV extends tv{public static void Main (string[] args) { LeTV LeTV = new LeTV (); Tv.show (LeTV); }}
see no, the program runs normally. Originally the parameter type is the TV type, but the subclass can also be passed in. The behavior of turning LeTV into a parent TV is to move up.
The upward transformation is secure, and subclasses must inherit the methods of the parent class (which is the definition of inheritance) and can own their own properties and methods, and it is safe to convert from a particular class to a generic class. In turn there are ordinary to special but there is no corresponding risk.
(2) polymorphic
First look at the example:
Class tv{ public Void Show () { System.out.println ("TV");} } Class LeTV extends tv{public Void Show () { System.out.println ("LeTV");} } Class MiTV extends tv{public Void Show () { System.out.println ("MiTV");} } Class Santv extends tv{public Void Show () { System.out.println ("Santv");} } public class Everytv {public static void Tvshow (LeTV TV) { tv.show (); } public static void Tvshow (MiTV TV) { tv.show (); } public static void Tvshow (Santv TV) { tv.show (); } public static void Main (string[] args) { tvshow (new LeTV ()); Tvshow (New MiTV ()); Tvshow (New Santv ());} }
There are no problems with the program, and each method has a corresponding type. But in addition to these three subcategories, there are dozens of, hundreds of TV sub-classes, then the Tvshow method is not to write the corresponding dozens of or even hundreds of?
Then, Polymorphic appears.
public class Everytv {public static void Tvshow (TV TV) { tv.show (); } public static void Main (string[] args) { tvshow (new LeTV ()); Tvshow (New MiTV ()); Tvshow (New Santv ());} }
The program is a lot more concise, and the result is the same as above. Beautiful a lot. Look again, this is like the upward transformation.
Polymorphic, can be understood into many forms, a variety of states. Polymorphism is also called dynamic binding, late binding, run-time binding.
When it comes to binding, it is necessary to contact the pre-binding (early binding domestic search is really miserable, put all the programming ideas into the words, will not really understand the pre-binding is what! )。
What is Early and late Binding? (from StackOverflow).
The short answer is a early (or static) binding refers to compile time binding and late (or dynamic) binding refers To runtime binding (for example if you use reflection)--byChristian hagelid.
It is simple to say that early binding is the binding of the compile period, and late binding is the binding at runtime.
The book shows that the method of C is all pre-binding, if the above polymorphic example is pre-binding, the compiler will be bound, but the TV reference only one, even if you pass in the LeTV, it does not know whether to execute the TV show method or other class of Show method.
Because of polymorphism, late binding exists, and the corresponding method is executed according to the object.
Because polymorphism exists, we can add multiple new subclasses without worrying about how many tvshow methods to write.
The upward transformation of the wording:
public static void Main (string[] args) { TV LeTV = new LeTV (); TV MiTV = new MiTV (); TV Santv = new Santv (); Tvshow (LeTV); Tvshow (MITV); Tvshow (SANTV); }
A wide variety of TVs or TVs can be transformed upward, because polymorphism will call the show method of their respective objects.
The exact words in the book are very good, and I'll copy that polymorphism is an important technique that allows programmers to "separate things from the unchanging."
For the above, the Show method of the respective TV has its own rewrite, but the Tvshow method within the Everytv class does not have to be changed. Is the usefulness of polymorphism.
Does that feel useless?
List al = new ArrayList (); List ll = new LinkedList ();
the use is here, I first use Al also line, LL also line, later to change can change, otherwise many things have to be changed.
(3) Defects
1) "Overwrite" private method
The example in the book is a bit farfetched, and the method is not rewritten.
public class tv{ private void Show () { System.out.println ("TV"); public static void Main (string[] args) { TV TV = new LeTV (); Tv.show (); }} Class LeTV extends tv{public Void Show () { System.out.println ("LeTV");} }
After learning the polymorphism, a look at the result is LeTV bar. Wrong, LeTV didn't rewrite the show method, the show method of the parent class is private, invisible, and not inheritable. False "Overwrite".
2) domain and static methods
For fields, the field concept is vague and can be seen: what is the domain in Java?
public class tv{Public int price = ten; public int GetPrice () { return price; } public static String getString () { return "TV"; }} Class LeTV extends tv{public int price =; public int GetPrice () { return price; } public int Getsuperprice () { return super.price; } public static String getString () { return "LeTV"; } public static void Main (string[] args) { TV TV = new LeTV (); System.out.println (tv.price+ "GetPrice:" +tv.getprice () +tv.getstring ()); LeTV LeTV = new LeTV (); System.out.println (letv.price+ "GetPrice:" +letv.getprice () + "Getsuperprice:" +letv.getsuperprice () + Letv.getstring ());} }
According to polymorphism, the result of Tv.price should be 20, but the result is 10, because the domain access operation is parsed by the compiler, not polymorphic, polymorphic is late bound, that is, during the run.
The Tv.getstring method is the same, and does not print out the LeTV, but the TV, because the method is a static method, only related to the class, and the specific object is not associated.
This is the two places to be aware of when using polymorphism.
Just start to see this polymorphic characteristics of the time ugly understand, do not know what the use of this thing, in fact, the program knocked more, its righteousness from the present. You will find that everything you write is used in the export.
Java programming Thought (v)-polymorphic (upper)