多态-II(接口实现)

时间:2022-01-31 13:59:35
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>多态类</title>
</head>
<body>
<?php
interface Car{
public function say();
}
class Bus implements Car{
public function say(){
echo "公交车"."<br>";
}
}
class Taxi implements Car{
public function say(){
echo "出租车"."<br>";
}
}
function say($obj){
if($obj instanceof Car){
$obj->say();
}
}
$bus=new Bus();
$taxi=new Taxi();
say($bus); //输出“公交车”
say($taxi); //输出“出租车”
?>
</body>
</html>