国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

簡(jiǎn)單工廠模式中的問題,總是報(bào)錯(cuò)?
phpcn_u814
phpcn_u814 2017-02-16 11:23:21
0
2
977
<?php
//接口
interface calc{
    public function getResult();
}
//運(yùn)算類
class Operation{
    protected $num1=0;
    protected $num2=0;
    protected $result=0;
    
    public function setNum($num1,$num2){
        $this->num1 = $num1;
        $this->num2 = $num2;
    }
}
//四則運(yùn)算類
class OperAdd extends Operation implements calc{
    public function getResult(){
        return $this->result=$this->num1+$this->num2;
    }
}
class OperSub extends Operation implements calc{
    public function getResult(){
        return $this->result=$this->num1-$this->num2;
    }    
}
class OperMul extends Operation implements calc{
    public function getResult(){
        return $this->result=$this->num1*$this->num2;
    }
}
class OperDiv extends Operation implements calc{
    public function getResult(){
        if(intval($this->num2)==0){
            return $this->result="被除數(shù)不能為‘0’!";
        }else{
            return $this->result=$this->num1/$this->num2;     
        }        
    }
}
class OperFactory{
    private static $obj;
    public static function createrOper($type){
        try {
            switch ($type){
                case'+';
                    self::$obj = new OperAdd();
                    break;
                case'-';
                    self::$obj = new OperSub();
                    break;
                case'*';
                    self::$obj = new OperMul();
                    break;
                case '/':
                    self::$obj = new OperDiv();
                    break;    
                    default:
                        throw new Exception('unknow type');
                }
            } catch (Exception $e) {
                echo $e->getMessage();
        }
    } 
}
$obj = OperFactory::createrOper('+');
$obj->setNum(2,2);
echo $obj->getResult();
// $obj= OperFactory::createrOper('+');
// $obj->setNum(2, 2);
// var_dump($obj);
// echo $obj->getResult();

問題就在 $obj->setNum(2,2); 這里,但是怎么看流程,都好像沒什么問題的

phpcn_u814
phpcn_u814

reply all(2)
數(shù)據(jù)分析師

Problems in simple factory model, errors always reported? -PHP Chinese website Q&A-Problems in simple factory mode, errors always reported? -PHP Chinese website Q&A

Let’s take a look and learn.

劉奇

$obj = OperFactory::createrOper('+');這個(gè)OperFactory的createOper方法沒有返回值,應(yīng)該return self::$obj = new OperAdd()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template