代码之家  ›  专栏  ›  技术社区  ›  Haris Khan

抽象封装和多态重载的区别

  •  0
  • Haris Khan  · 技术社区  · 6 年前

    我正在阅读关于这些术语的不同文章,但我无法理解这些术语之间的实际差异。我需要一些真实的例子,例如一些代码例子,来理解抽象和封装是如何工作的。 还有人请告诉我多态性和重载之间的区别。我们将非常感谢您的帮助。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Asiya Fatima    6 年前

    嗨,你可以试着读一读这个也许它会有帮助:

     Short answer: 
      They are the same. 
    
     Long Answer, (and yet less revealing): 
    
    
      Polymorphism is simply the ability to have many different methods (Or functions, 
     for those who are used to C-Type programs) to have the same name but act differently 
      depending on the type of parameters that were passed to the function. 
    
     So for example, we may have a method called punch, which accepts no parameters at 
       all, 
     and returns an integer: 
    
      public int punch() 
     { 
      return 3; 
    } 
    
    
      We could also have a method named punch that accepts a String and returns a 
    boolean. 
    
    public boolean punch(String poorGuyGettingPunched) 
    { 
     if(poorGuyGettingPunched.equals("joe")) 
    { 
     System.out.println("sorry Joe"); 
     return true; 
     } 
     else 
       return false; 
       } 
    
     That is called polymorphism... And strangely enough, it is also called overloading.