zoukankan      html  css  js  c++  java
  • java中的多态性

    多态性

           多态(Polymorphism),也是面向对象的基本特征之一,前面的函数重载是静态多态,还有一种多态是动态多态,理论基础是父类引用可以指向子类对象。如果说静态多态可以使同一个名称拥有不同功能、执行不同操作,那么动态多态就是使父类引用动态的指向多个子类对象,解决了静态多态性需要编写多个函数实现不同功能的缺点。举个例子:

    静态多态(函数重载):

     1 package poly1;
     2 class Dialog {
     3      public void show(){
     4            System.out.println("Dialog.show()");
     5      }
     6 }
     7 class FontDialog extends Dialog{
     8      public void show(){
     9            System.out.println("FontDialog.show()");
    10       }
    11 }
    12 class ParagraphDialog extends Dialog{
    13      public void show(){
    14            System.out.println("ParagraphDialog.show()");
    15       }
    16 }
    17 public class Main {
    18       public static void toCenter(FontDialog fd){
    19             System.out.println("计算屏幕数据");
    20             fd.show();
    21        }
    22        public static void toCenter(ParagraphDialog pd){
    23             System.out.println("计算屏幕数据");
    24             pd.show();
    25       }
    26       public static void main(String[] args){
    27             FontDialog fd = new FontDialog();
    28             toCenter(fd);
    29             ParagraphDialog pd = new ParagraphDialog();
    30             toCenter(pd);
    31       }
    32 }

    动态多态:

    package poly2;
    class Dialog {
         public void show(){
               System.out.println("Dialog.show()");
         }
    }
    class FontDialog extends Dialog{
         public void show(){
               System.out.println("FontDialog.show()");
          }
    }
    class ParagraphDialog extends Dialog{
         public void show(){
               System.out.println("ParagraphDialog.show()");
          }
    }
    public class Main {
          public static void toCenter(Dialog dialog){
                System.out.println("计算屏幕数据");
                dialog.show();
         }
         public static Dialog fun(){
               return new FontDialog();
         }
          public static void main(String[] args){
                Dialog fd = fun();//函数的返回类型是父类类型,实际返回的可以是子类对象
                toCenter(fd);
                Dialog pd = new ParagraphDialog();//子类对象直接赋值给父类引用
                toCenter(pd);
          }
    }

    通过上面代码我们可以看到,静态多态因参数不同要写出许多重载函数,而动态多态的参数只需要是他们的父类即可。不仅如此动态多态函数的返回类型是父类类型,实际返回的可以是子类对象,这个作用我们可以在代码中看的出来:main函数中根本没有 FontDialog 类的痕迹,main 函数仅仅需要认识 Dialog 类,就能够调用 Dialog 的所有不同子类的函数,而不需要知道这些函数是怎么实现的。修改时返回对象时根本不用对main函数进行更改。

    父子类对象的类型转换

            1、子类类型对象转换成父类类型:子类对象无需转换,就可以赋值给父类引用。如:Dialog dialog = new FontDialog();

       2、父类类型对象转换成子类类型。只有在父类类型对象原来就是某一种子类类型的对象时可进行强制转换,如

           Dialog dialog = new FontDialog();

           FontDialog fd = (FontDialog)dialog;

     附:检验对象实际上是什么类型可以使用 instanceof 操作符来进行判断。

                System.out.println(对象名 instanceof 类名);

  • 相关阅读:
    mysql备份监控脚本
    关于ibatis的缓存的
    一些好用的软件和工具
    一,关于mongDB+SpringMVC【org.springframework.data.mongodb.core.MongoTemplate】
    java中的String内存分配原理
    java条件结构
    java运算符与选择结构
    JAVA变量补充
    JAVA变量
    java基础01
  • 原文地址:https://www.cnblogs.com/541wsy/p/12518449.html
Copyright © 2011-2022 走看看