- <?php
- /*
- * Created on 2011-5-16
- *
- * To change the template for this generated file go to
- * Window - Preferences - PHPeclipse - PHP - Code Templates
- */
- abstract class car{
- abstract function getMaxSpeed();
- }
- class FastCar extends car{
- function getMaxSpeed()
- {
- return 500;
- }
- }
- class SlowCar extends car{
- function getMaxSpeed()
- {
- return 50;
- }
- }
- class Street{
- protected $speedLimit;
- protected $cars;
- public function __construct($speedLimit=200)
- {
- $this->cars=array();
- $this->speedLimit=$speedLimit;
- }
- protected function islegal($car)
- {
- if($car->getMaxSpeed() < $this->speedLimit)
- return true;
- else
- return false;
- }
- public function addcars($car)
- {
- if($this->islegal($car))
- {
- echo "this car is allowed to run in the street";
- $this->cars[]=$car;
- }else{
- echo "it's fobidden to run";
- }
- }
- public function getcarslist()
- {
- print_r($this->cars);
- }
- }
- $b=new street();
- $b->addcars(new FastCar);
- echo "<br>";
- $b->addcars(new SlowCar);
- $b->addcars(new SlowCar);
- $b->addcars(new SlowCar);
- $b->addcars(new SlowCar);
- $b->addcars(new SlowCar);
- $b->getcarslist();
- ?>
转载于:https://blog.51cto.com/bearonline/566520