日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php总是报错,php - 简单工厂模式中的问题,总是报错

發布時間:2023/12/15 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php总是报错,php - 简单工厂模式中的问题,总是报错 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php - 簡單工廠模式中的問題,總是報錯

PHPzhong2017-04-11 09:53:49 0 3 101

//接口

interface calc{

public function getResult();

}

//運算類

class Operation{

protected $num1=0;

protected $num2=0;

protected $result=0;

public function setNum($num1,$num2){

$this->num1 = $num1;

$this->num2 = $num2;

}

}

//四則運算類

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 Operp extends Operation implements calc{

public function getResult(){

if(intval($this->num2)==0){

return $this->result="被除數不能為‘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 Operp();

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); 這里,但是怎么看流程,都好像沒什么問題的

回答

3

0

分享

全部回復 (3)

大家講道理2017-04-11 09:55:493樓

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

回復

迷茫2017-04-11 09:55:492樓

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 Operp();

break;

default:

throw new Exception('unknow type');

}

} catch (Exception $e) {

echo $e->getMessage();

}

return self::$obj;

}

回復

巴扎黑2017-04-11 09:55:491樓

建議去翻譯下設計模式的資料設計模式,希望對你有幫助

回復

總結

以上是生活随笔為你收集整理的php总是报错,php - 简单工厂模式中的问题,总是报错的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。