class yunsuan { public $a; public $b; function suan() { echo "对两个数进行运算"; } } class jia extends yunsuan { function suan() { return $this->a+$this->b; } } class jian extends yunsuan { function suan() { return $this->a-$this->b; } } class cheng extends yunsuan { function suan() { return $this->a*$this->b; } } class chu extends yunsuan { function suan() { return $this->a/$this->b; } } //对扩展开放,对修改封闭 //工厂类 class gongchang { //给一个参数,返回一个对象 static function chanpin($name) { switch ($name) { case"+": return new jia(); break; case"-": return new jian(); break; case"*": return new cheng(); break; case"/": return new chu(); break; } } } //$a=gongchang::chanpin("+"); //var_dump($a); //$a=gongchang::chanpin("-"); //var_dump($a); //$a=gongchang::chanpin("*"); //var_dump($a); $a=gongchang::chanpin("/"); var_dump($a);