抽象-封装与多态-过载之间的区别

时间:2018-12-04 06:25:17

标签: php oop polymorphism overloading encapsulation

我正在阅读有关这些术语的不同文章,但是我无法理解这些术语之间的实际差异。我需要一些真实的示例,例如一些代码示例,以了解抽象和封装的工作原理。 也有人请告诉我多态与重载之间的区别。帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

您好,您可以尝试阅读以下内容,这可能会有所帮助:

 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. 

答案 1 :(得分:0)

请参阅此链接,希望对您有所帮助     difference between polymorphism and overloading超载