instanceof的使用

<?php
header("content-type:text/html;charset=utf-8");
 interface Drive{
  public function back();
  public function go();
 }
 class Car implements Drive{
  public function go(){
   echo "我开车往前走";
  }
  public function back(){
   echo"我开车往回走";
  }
 }
  class Bike implements Drive{
   public function go(){
    echo "我往前骑车";
   }
   public function back(){
    echo "我往回骑车";
   }
  }
  function goForward($obj){
   if($obj instanceof Drive){
    $obj->back();
   }
  }
   $tool1=new Car();
   $tool2=new Bike();
   goForward($tool2);
?>

    A+
发布日期:2014年07月10日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: