失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > PHP面向对象-多态

PHP面向对象-多态

时间:2019-09-26 00:54:55

相关推荐

PHP面向对象-多态

在面向对象中,多态指的是多个函数使用同一个名字,但是参数个数,个数数据类型不同,调用时虽然方法名相同,但会根据参数个数或类型自动调用对应的函数。

代码:

<?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面向对象-多态》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。