interface ISpeedInfo
{
function getMaximumSpeed();
}
class Car{
}
class FastCar extends Car implements ISpeedInfo
{
function getMaximumSpeed()
{
return 150;
}
}
class Street
{
protected $speedLimit;
protected $cars;
public function __construct($speedLimit = 200)
{
$this->cars = array();
$this->speedLimit = $speedLimit;
}
protected function isStreetLegal($car)
{
if($car instanceof ISpeedInfo){
if($car->getMaximumspeed()<$this->speedLimit)
{
return ture;
} else {
//扩展类必须实现ISpeedInfo才能使Street合法
return false;
}
return false;
}
}
public function addCar($car)
{
if($this->isStreetLegal($car))
{
echo 'The Car was allowed on the road.';
$this->cars[] = $car;
} else {
echo 'The Car is too fast and was not allowed on the road.';
}
}
}
$street = new Street();
$street->addCar(new FastCar());
?>
输出 The Car was allowed on the road.
intanceof:返回一个BOOLEAN类型的值。用来确定对象的某个实例是否为特定的类型,或者是否从某个类型继承,又或者是否实现了某个特定的接口。
阅读(543) | 评论(0) | 转发(0) |