What is polymorphism?
Polymorphism literally means "multiple states". In Object-oriented languages, multiple different implementations of interfaces are called polymorphism. Quoting Charlie Calverts' description of polymorphism - Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its child objects. After assignment, the parent object can be assigned to its child objects based on the current value. features operate in different ways (from "Insider Delphi 4 Programming Technology"). To put it simply, it is one sentence: It is allowed to assign a pointer of a subclass type to a pointer of a parent class type (yes, this passage comes from Baidu Encyclopedia). So what is the role of polymorphism, and what is its actual development value? In actual application development, the main purpose of using object-oriented polymorphism is that different subclass objects can be treated as one parent class, and the differences between different subclass objects can be shielded and universal objects can be written. code, making general programming to adapt to changing needs.
The following are two implementations of polymorphism in PHP
Method overloading( overload)
Overloading is an implementation of polymorphism of the class. Function overloading means that an identifier is used as multiple function names, and these functions with the same name can be distinguished by the number of parameters or parameter types of the function, so that no confusion occurs when calling. That is, when called, although the method names are the same, the corresponding function can be automatically called according to the different parameters.
class A{ public function test(){ echo "test1"; } public function test($a){ echo "test2"; } } $a=new A(); $a->test(); $a->test($a);
public function test($a){
public function call($method,$p) { if($method=="display"){ if(is_object($p[0])){ $this->displayObject($p[0]); }else if(is_array($p[0])){ $this->displayArray($p[0]); }else{ $this->displayScalar($p[0]); } } } //下面是对上面定义的调用 $ov=new overload; $ov->display(array(1,2,3)); $ov->display('cat');
Method override (override)
There are the following requirements:
The following is an explanation of these points:
class people{ protected function sing(){ echo "人唱歌"; } } class woman extends people{ public function sing(){ echo "女人唱歌"; } } $woman1=new woman(); $woman1->sing();
第三点,是要求参数和名字一样,具体就是要求参数的个数与父类相同,而并不是参数名称一致。即传递的参数名字可以为任意,只要保证传递的个数相同即可。
以上内容简单介绍了PHP语言中多态的两个实现。
PS:重写、覆盖、重载、多态几个概念的区别分析
override->重写(=覆盖)、overload->重载、polymorphism -> 多态
override是重写(覆盖)了一个方法,以实现不同的功能。一般是用于子类在继承父类时,重写(重新实现)父类中的方法。
重写(覆盖)的规则:
1、重写方法的参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.
2、重写方法的访问修饰符一定要大于被重写方法的访问修饰符(public>protected>default>private)。
3、重写的方法的返回值必须和被重写的方法的返回一致;
4、重写的方法所抛出的异常必须和被重写方法的所抛出的异常一致,或者是其子类;
5、被重写的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行重写。
6、静态方法不能被重写为非静态的方法(会编译出错)。
overload是重载,一般是用于在一个类内实现若干重载的方法,这些方法的名称相同而参数形式不同。
重载的规则:
1、在使用重载时只能通过相同的方法名、不同的参数形式实现。不同的参数类型可以是不同的参数类型,不同的参数个数,不同的参数顺序(参数类型必须不一样);
2、不能通过访问权限、返回类型、抛出的异常进行重载;
3、方法的异常类型和数目不会对重载造成影响;
多态的概念比较复杂,有多种意义的多态,一个有趣但不严谨的说法是:继承是子类使用父类的方法,而多态则是父类使用子类的方法。
一般,我们使用多态是为了避免在父类里大量重载引起代码臃肿且难于维护。
举个例子:
public class Shape { public static void main(String[] args){ Triangle tri = new Triangle(); System.out.println("Triangle is a type of shape? " + tri.isShape());// 继承 Shape shape = new Triangle(); System.out.println("My shape has " + shape.getSides() + " sides."); // 多态 Rectangle Rec = new Rectangle(); Shape shape2 = Rec; System.out.println("My shape has " + shape2.getSides(Rec) + " sides."); //重载 } public boolean isShape(){ return true; } public int getSides(){ return 0 ; } public int getSides(Triangle tri){ //重载 return 3 ; } public int getSides(Rectangle rec){ //重载 return 4 ; } } class Triangle extends Shape { public int getSides() { //重写,实现多态 return 3; } } class Rectangle extends Shape { public int getSides(int i) { //重载 return i; } }
注意Triangle类的方法是重写,而Rectangle类的方法是重载。对两者比较,可以发现多态对重载的优点:
如果用重载,则在父类里要对应每一个子类都重载一个取得边数的方法;
如果用多态,则父类只提供取得边数的接口,至于取得哪个形状的边数,怎样取得,在子类里各自实现(重写)。
The above is the detailed content of How PHP understands method overloading and polymorphic method coverage. For more information, please follow other related articles on the PHP Chinese website!