About "Binding" technology

zhaozj2021-02-16  55

Open "Thinking In Java Se", after reading the type of idiom declaration, the interest is caused, open the seventh chapter, revisit "Polymorphism). "Thinking in Java Se" Seventh chapter - "Polymorphism" is the so-called "polymorphism": inheritance mechanism allows you to not only make some objects, it can also be used in its own type ( Base Type, depending on the coupling relationship between the type. (Original) Upcasting: You can Upcasting but not Downcasting. Example: (Upcasting) Class A () {public void play () {system.out.println ("a is playing!");}}

Class b () {public void play () {system.out.println ("a is playing!");}}} public class test () {public static void tune (a a a a a a) {a.play ();} public Static void main (stiring [] args) {a a = new a (); b = new b (); tune (a); tune (b); // upcasting, tune () method requires a Type A type object, The B-type object is lacking here, so the B type object is automatically transformed into a type A object because B is inherited from A, B is an A}}}: (Downcasting) Import java.util. *; Public Class TestarrayList ) {Public static void main (string [] args) {arraylist al = new arraylist (); string s = "a"; al01.add (s); al01.add ("b"); al01.add ("c "); //! ~ Arraylist al02 = al.clone (); error, because Clone () returns an Object object, Object is ArrayList's base class, cannot let Object Downcasting are an ArrayList. ArrayList Al02 = (arraylist) al.clone (); // is feasible, use () for mandatory type conversion}}

转载请注明原文地址:https://www.9cbs.com/read-21623.html

New Post(0)