<?php
interface ICanEat{
public function eat($food);
}
class Human implements ICaneat{
public function eat($food){
echo "Human eating".$food." ";
}
}
class Animal implements ICaneat{
public function eat($food){
echo "Animal eating".$food." ";
}
}
//在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
?>