简单工厂模式编写计算器小程序实现简单工厂模式ref=newReflectionObject($this);}publicfunction__set($name,$value){if($this->ref->hasProperty("$name")){$this->$name=$value;}}publicfunction__get($name){if($this->ref->hasProperty("$name")){return$this->$name;}}公共函数getResutl(){返回0;}}//子类classAddextendsOperation{publicfunctiongetResutl(){return$this->_num1+$this->_num2;}}classSubextendsOperation{publicfunctiongetResutl(){return$this->_num1-$this->_num2;}}classMulextendsOperation{publicfunctiongetResutl(){return$this->_num1*$this->_num2;}}classDivextendsOperation{publicfunctiongetResutl(){if(empty($this->_num2)){thrownewException("除数不能为0\n");}返回$this->_num1/$this->_num2;}}classOperationFactory{publicstaticfunctioncreateOperate($oper){$res=null;开关($oper){case'+':$res=newAdd();休息;案例'-':$res=newSub();休息;case'*':$res=newMul();休息;case'/':$res=newDiv();休息;default:thrownewException("操作错误\n",1);休息;}返回$res;}}/*//mainprogramtry{//单独实例化,需要//公共实例化->简单工厂模式$a=newDiv();$a->_num1=11;$a->_num2=0;var_dump($a->getResutl());}catch(Exception$e){回显$e->getMessage();}*///使用简单的工厂模式try{$oper=OperationFactory::createOperate('+');$oper->_num1=11;$oper->_num2=2;echo$oper->getResutl();}catch(Exception$e){echo$e->getMessage();}/***认为:java是强类型语言,父类的引用指向实例子类的*而php不是*/
