Home >Java >javaTutorial >What is polymorphism? Polymorphic life examples

What is polymorphism? Polymorphic life examples

PHP中文网
PHP中文网Original
2017-06-20 15:35:254336browse

Java Basics Eleven - Polymorphism

1. Polymorphism Definition

Simply put: it is an object corresponding to different types.

Polymorphism in the code Reflection:
The reference of the parent class or interface points to the object of its subclass.

  1 /*  2   3 对象的多态性。  4   5 class 动物  6 {}  7   8 class 猫 extends 动物  9 {} 10  11 class 狗 extends 动物 12 {} 13  14  15  16 猫 x = new 猫(); 17  18 动物 x = new 猫();//一个对象,两种形态。 19  20  21  22 猫这类事物即具备者猫的形态,又具备着动物的形态。 23 这就是对象的多态性。 
 24  25 简单说:就是一个对象对应着不同类型.  26  27 多态在代码中的体现:
 28     父类或者接口的引用指向其子类的对象。 29  30  31 多态的好处: 32     提高了代码的扩展性,前期定义的代码可以使用后期的内容。 33  34 多态的弊端: 35     前期定义的内容不能使用(调用)后期子类的特有内容。通过向下转型来解决。 36  37 多态的前提: 38     1,必须有关系,继承,实现。(实现是特殊的继承) 39     2,要有覆盖。 
 40  41  42  43 */ 44  45 abstract class Animal 46 { 47     abstract void eat(); 48  49 } 50  51 class Dog extends Animal 52 { 53     void eat() 54     { 55         System.out.println("啃骨头"); 56     } 57     void lookHome() 58     { 59         System.out.println("看家"); 60     } 61 } 62  63 class Cat extends Animal 64 { 65     void eat() 66     { 67         System.out.println("吃鱼"); 68     } 69     void catchMouse() 70     { 71         System.out.println("抓老鼠"); 72     } 73 } 74  75 class Pig extends Animal 76 { 77     void eat() 78     { 79         System.out.println("饲料"); 80     } 81     void gongDi() 82     { 83         System.out.println("拱地"); 84     } 85 } 86  87  88  89 class DuoTaiDemo 
 90 { 91     public static void main(String[] args) 
 92     { 93          94 //        Cat c = new Cat(); 95 //        c.eat(); 96 //        c.catchMouse(); 97  98         Animal a = new Cat(); //自动类型提升,猫对象提升了动物类型。但是特有功能无法s访问。
 99                             //作用就是限制对特有功能的访问。
100                             //专业讲:向上转型。将子类型隐藏。就不用使用子类的特有方法。101 102 103 //        a.eat();104 105         //如果还想用具体动物猫的特有功能。 
106         //你可以将该对象进行向下转型。107 //        Cat c = (Cat)a;//向下转型的目的是为了使用子类中的特有方法。108 //        c.eat();109 //        c.catchMouse();110 111 //        注意:对于转型,自始自终都是子类对象在做着类型的变化。112 //        Animal a1 = new Dog();113 //        Cat c1 = (Cat)a1;//ClassCastException114 115 116         /*117         Cat c = new Cat();118 119 //        Dog d = new Dog();120 121 //        c.eat();122         method(c);123 //        method(d);124 //        method(new Pig());125         */126 127         method(new  Dog());128 129     }130 131     public static void method(Animal a)//Animal a = new Dog();132     {133         a.eat();134      //解决类型匹配问题的时候,我们就可以判断一下135         if(a instanceof Cat)//instanceof:用于判断对象的具体类型。只能用于引用数据类型判断
136 //                        //通常在向下转型前用于健壮性的判断。137 138         {139             Cat c = (Cat)a;140             c.catchMouse();141         }142         else if(a instanceof Dog)143         {144             Dog d = (Dog)a;145             d.lookHome();146         }147         else148         {149         150         }151         152     }153     /*154     public static void method(Cat c)155     {156         c.eat();157     }158     public static void method(Dog d)159     {    
160         161     }162     */    163 }

Upward transformation: The parent class reference points to the subclass object. Subclass-specific functionality is inaccessible.

Downcast: The subclass reference points to the parent class object.

1 Animal a = new Cat(); //自动类型提升,猫对象提升了动物类型。但是特有功能无法s访问。2                       //作用就是限制对特有功能的访问。3                       //专业讲:向上转型。将子类型隐藏。就不用使用子类的特有方法。4 5 Cat c = (Cat)a;//向下转型的目的是为了使用子类中的特有方法。

In practical applications, upward transformation facilitates code expansion (the code written before can be used in the future, only if it inherits or implements the base class), but the unique features of the subclass must be used When functional, it must be transformed downward.

Many times we cast upward to the Object class, and when our own unique functions are used, we cast downward and back.

2. Examples of polymorphic life

 1 /* 2 毕老师和毕姥爷的故事。 3 */ 4  5 class 毕姥爷 6 { 7     void 讲课() 8     { 9         System.out.println("管理");10     }11     void 钓鱼()12     {13         System.out.println("钓鱼");14     }15 }16 17 class 毕老师 extends 毕姥爷18 {19     void 讲课()20     {21         System.out.println("Java");22     }23     void 看电影()24     {25         System.out.println("看电影");26     }27 }28 29 30 31 32 33 class  DuoTaiDemo234 {35     public static void main(String[] args) 
36     {   //原来37 //        毕老师 x = new 毕老师();38 //        x.讲课();39 //        x.看电影();40      //多态41         毕姥爷 x = new 毕老师();42         x.讲课(); //这里讲的是Java的内容,Java把管理学覆盖了 43         x.钓鱼();44 45         毕老师 y = (毕老师)x;//ClassCastException46         y.看电影();47 48 49 50 51     }52 }

The above is the detailed content of What is polymorphism? Polymorphic life examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn