Java notes 3 polymorphism, java Polymorphism

Source: Internet
Author: User

Java notes 3 polymorphism, java Polymorphism
Polymorphism: --- multiple forms of an object essence: the reference of the parent class points to the subclass object --- premise: must inherit or implement class Animal {} class Dog extends Animal {} main {Dog d = new Dog (); // This state Animal dog = new Dog (); // polymorphism} Note: objects created in the form of polymorphism can only access member Methods common to sub-parent classes. The running result is that the sub-class result is a member method unique to the sub-class and cannot be used. Virtual method call: the unique method of the parent class object to call the sub-class Student S = new Student ("Zhang San", 100); Person p = new Student ("Li Si", 200); System. out. println (S. say (); System. out. println (p. say (); multi-state array-in an array of reference types, objects are stored in the form of polymorphism. Eg: Person [] person = {new Person ("Zhang San", 32), new Student ("Li Si", 21,120, 90.0), new Student ("Wang Wu", 22,119, 91.5), new Teacher ("instructor Liu", 35, 10, "Java EE"), new Teacher ("instructor Zhang", 11 )}; polymorphism parameter-reference type parameter instanceof operator in the method parameter list-determines whether an object is of the specified type (form) forced type conversion-converts an object from one type of reference to another type of reference object Association: one object uses one-to-one, one-to-many, many to one class test4 {public static void main (String [] args) {Person p = new Person ("", 50); method (p ); student S = new Student ("Michael", 100); method (S); Person p1 = new Student ("Zhao Si", 100); method (p1 );} public static void method (Person p) {if (p instanceof Student) {System. out. println ("instructor"); Student s = (Student) p; System. out. println ("local method call:" + 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 () + "student id:" + id + "score:" + score ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.