Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160997
  • 博文数量: 52
  • 博客积分: 2295
  • 博客等级: 大尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-22 17:42
文章分类

全部博文(52)

文章存档

2013年(1)

2012年(11)

2011年(1)

2010年(31)

2009年(8)

我的朋友

分类:

2010-01-20 14:08:38

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类型的值。用来确定对象的某个实例是否为特定的类型,或者是否从某个类型继承,又或者是否实现了某个特定的接口。
阅读(513) | 评论(0) | 转发(0) |
0

上一篇:abstract抽象类

下一篇:OOP的单例模式

给主人留下些什么吧!~~