在面向对象中,多态指的是多个函数使用同一个名字,但是参数个数,个数数据类型不同,调用时虽然方法名相同,但会根据参数个数或类型自动调用对应的函数。
代码:
<?phpclass Animal{function __construct($name){$this->name=$name;}function show(){echo "$this->name is an animal<br/>";}}class Dog extends Animal{function show(){echo "$this->name is a dog<br/>";}}class Cat extends Animal{function show(){echo "$this->name is a cat<br/>";}}function display($obj){if($obj instanceof Animal){$obj->show();}elseecho "none";}display(new Animal("xx"));display(new Dog("xx"));display(new Cat("xx"));?> /*运行结果*//*xx is an animalxx is a dogxx is a cat*/
如果觉得《PHP面向对象-多态》对你有帮助,请点赞、收藏,并留下你的观点哦!