多态性是面向对象的语言的一个主要特色,因为C++为了照顾C的原因,让其成为了一个不彻底的面向对象,而Java作为一门完全的面向对象语言,故而在多态性这点上有其独特的特色。本文就我自己对Java多态性的理解,谈了一些自己的看法,然后写了点代码表达一下。


     总的来说,Java的多态性通过两种方式来表现,一种是静态,另一种是动态。假设现在有两个类,父类为Parent,子类为Child,两个类中都有方法func。


Java代码

为什么java中fun函数 java中fun是什么意思_为什么java中fun函数



    1. class
    2. public void
    3. "In the Parent's function ");    
    4.     }   
    5.        
    6. }   
    7.   
    8. class Child extends
    9. //覆盖父类的方法时 
    10. public void
    11. "In the Child's function, overriding ");    
    12.     }   
    13.        
    14. //重载父类的方法 
    15. public void
    16. "In the Child's function, overloading");    
    17.     }   
    18. }



    class Parent {
        public void func(String str) {
            System.out.println("In the Parent's function ");
        }
        
    }
    
    class Child extends Parent {
        //覆盖父类的方法时
        public void func(String str) {
            System.out.println("In the Child's function, overriding ");
        }
        
        //重载父类的方法
        public void func() {
            System.out.println("In the Child's function, overloading");
        }
    }


     

         静态而言就是这样的,用一个父类的引用引用父类的一个对象,如Parent p = new Parent(); 再用子类的一个引用指向一个子类的对象,如 Child c = new Child(); 分别调用父类跟子类的方法时(通过p跟c)都是调用本身类中的方法。如执行p.func("Parent"); c.func("Child"); 时分别调用父类和子类中的方法。

         对于动态而言,我个人觉得可以这样理解,因为动态调用要求在子类覆盖父类中对应的方法,也就是说,在子类中已经看不到父类中的那个方法了,调用时采用super.func(参数); 那么再调用该方法时就是调用子类的这个方法了。当然采用动态调用时必须得用一个父类的引用来引用一个子类的对象,如Parent p2 = new Child(); 才能调用子类中的方法,如果是父类的引用指向一个父类的对象,就会调用的是父类的方法,也就是说,要实现动态调用,必须得是一个父类的引用来引用一个子类或者父类的对象。

         一直以来,我都觉得子类中不能同时存在两个同名方法,一个是对父类相应方法的overriding,一个对父类对应方法的overloading。但前天一写代码实现才发现这样是行得通的,下面是我写的代码跟相应的输出结果,个人表达能力有限,就不解释原因了。


    <!---->class  Parent {
         public   void  func(String str) {
            System.out.println( " In the Parent's function  " );
        }
        
    }
    
    class  Child  extends  Parent {
         // 覆盖父类的方法时 
         public   void  func(String str) {
            System.out.println( " In the Child's function, overriding  " );
        }
        
         // 重载父类的方法 
         public   void  func() {
            System.out.println( " In the Child's function, overloading " );
        }
    }
    
    public   class  OverrindAndOverload {
         public   static   void  main(String[] args) {
            Parent p  =   new  Parent();
            p.func( " Parent " );     // In the Parent's function  
            
            Child c1  =   new  Child();
            c1.func( " Child " );     // In the Child's function, overriding  
            
            Child c2  =   new  Child();
            c2.func();             // In the Child's function, overloading 
            
            Parent dp  =   new  Child();
            dp.func( " who " );         // In the Child's function, overriding  
        }
    }


    Java代码

    为什么java中fun函数 java中fun是什么意思_为什么java中fun函数


    1. class
    2. public void
    3. "In the Parent's function ");    
    4.     }   
    5.        
    6. }   
    7.   
    8. class Child extends
    9. //覆盖父类的方法时 
    10. public void
    11. "In the Child's function, overriding ");    
    12.     }   
    13.        
    14. //重载父类的方法 
    15. public void
    16. "In the Child's function, overloading");    
    17.     }   
    18. }   
    19.   
    20. public class
    21. public static void
    22. new
    23. "Parent");    //In the Parent's function  
    24.            
    25. new
    26. "Child");    //In the Child's function, overriding  
    27.            
    28. new
    29. //In the Child's function, overloading 
    30.            
    31. new
    32. "who");        //In the Child's function, overriding  
    33.     }   
    34. }