Polymorphism:---The various morphological essence of an object: the reference of the parent class points to the subclass object---Premise: there must be an inheritance or implement class Animal{}class dog extends Animal{}main{dog d = new Dog ();//Native Animal dog = new Dog (); Polymorphic} Note: objects created by polymorphic forms can only access member methods that are common to the child parent class, and the result is a subclass-specific member method that cannot be used by the child class. Virtual method Invocation: The parent class object calls the subclass-specific method Student S = new Student ("Zhang San", 100); Person p = new Student ("John Doe", 200); System.out.println (S.say ()); System.out.println (P.say ()); polymorphic array-In an array of reference types, the object is stored in polymorphic form. Eg:person[] person = {New Person ("Zhang San", +), new Student ("John Doe", +, +, 90.0), New Student ("Harry", 119, 91.5), New Teacher (" Ms. Liu ", (+)," Java EE "), New Teacher (" Mr. Zhang ")}; polymorphic Parameters-the reference type parameter in the method parameter list instanceof operator-Determines whether an object is coerced type conversion for a specified type (pattern)-objects from One of the reference patterns is converted to another reference morphological Object association: One object is used with another object. Single-to-many, many-to-one class Test4 {public static void main (string[] args) { person p = new Person ("Harry", "N"); method (P); Student s = new Student ("Zhang San", +); method (s); person P1 = new Student ("Zhao Si", "n"); method (p1);} public static void Method (person p) {if (P instanceof Student) {System.out.println ("teacher"); Student s = (Student) p; System.out.println ("state method invocation:"+p.say ());} Else{system.out.println ("person");} system.out.println (P.say ());}} class person{private string name;private int age; public person () {} public person (String Name,int age) { THIS.name = Name;this.age = age;} public String GetName () {return name;} public String Say () {return "name:" +name+ "Age:" +age;}} class Student extends person{private int id;private double score; public Student (String name,int ID) {This ( name,20,id,98.5);} public Student (String name,int age,int id,double score) {super (name,age); this.id = Id;this.score = score;} public Double getId () {return ID;} public String Say () {return Super.say () + "study Number:" +id+ "score:" +score;}}
Java Handy Note three polymorphism