【发布时间】:2018-12-04 06:25:17
【问题描述】:
我正在阅读有关这些术语的不同文章,但我无法理解这些术语之间的实际区别。我需要一些真实的示例,例如一些代码示例,以了解抽象和封装是如何工作的。 有人也请告诉我多态性和重载之间的区别。非常感谢您的帮助。
【问题讨论】:
标签: php oop polymorphism overloading encapsulation
我正在阅读有关这些术语的不同文章,但我无法理解这些术语之间的实际区别。我需要一些真实的示例,例如一些代码示例,以了解抽象和封装是如何工作的。 有人也请告诉我多态性和重载之间的区别。非常感谢您的帮助。
【问题讨论】:
标签: php oop polymorphism overloading encapsulation
您好,您可以尝试阅读这篇文章,也许会有所帮助:
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.
【讨论】: