//设计模式:工厂模式 /*class suanshu { public $a; public $b; function jia() { return $this->a+$this->b; } function jian() { return $this->a-$this->b; } function cheng() { return $this->a*$this->b; } function chu() { return $This->a/$this->b; } function quyu() { return $this->a%$this->b; } } $r=new suanshu(); $r->a="5"; $r->b=9; echo $r->quyu();*/ //大类分子类 class suanshu { public $a; public $b; function yunsuan(){} } class jia extends suanshu { function yunsuan() { return $this->a+$this->b; } } class jian extends suanshu { function yunsuan() { return $this->a-$this->b; } } class cheng extends suanshu { function yunsuan() { return $this->a*$this->b; } } $r=new jia(); $r->a=1; $r->b=1; echo $r->yunsuan()."<br />"; $r=new jian(); $r->a=1; $r->b=1; echo $r->yunsuan(); //工厂类 对创建的对象进行优化,不用记住对象名,只需要记住符号或是代号就好。 class gongchang { static function duixiang($a) { switch($a) { case "+": return new jia(); break; case "-": return new jian(); break; case "*": return new cheng(); break; case "/": return new chu(); break; } } } $r=gongchang::duixiang("-");//重点是这句话 $r->a=10; $r->b=5; echo $r->yunsuan();