php笔记(七)PHP类于对象之多态
<?php
interface ICanEat{
public function eat($food);
}
class Human implements ICaneat{
public function eat($food){
echo "Human eating".$food."\n";
}
}
class Animal implements ICaneat{
public function eat($food){
echo "Animal eating".$food."\n";
}
}
//在ICanEat接口下的eat()方法,在Human类和Animal类中实现的结果是不一样的,这就是多态
$person = new Human();
$person->eat('apple');
$animal = new Animal();
$animal->eat('banana');
function checkeat($person){
if($person instanceof ICanEat){
$persong->eat('food');
}else{
echo "not find";
}
}
//相同的一行代码,对于传入不同的接口的实现对象的时候,表现是不同的,这就是多态
checkeat($person);
checkeat($animal);
输出的结果就是
Human eating food
Animal eating food
?>
个人学习随笔,不一定原创,不定时更新
[gitHub]: https://github.com/MicahZJ
[博客]: https://micahzj.github.io/MicahZJ.github.io/
[掘金]: https://juejin.im/user/5c1355ebe51d4521030cfe84/posts/
有关问题,或者想和本人讨论
欢迎通过本人QQ872219020联系
或者在github上提issues,虽然我不一定会去看o
[gitHub]: https://github.com/MicahZJ
[博客]: https://micahzj.github.io/MicahZJ.github.io/
[掘金]: https://juejin.im/user/5c1355ebe51d4521030cfe84/posts/
有关问题,或者想和本人讨论
欢迎通过本人QQ872219020联系
或者在github上提issues,虽然我不一定会去看o