PHP-----面向对象的设计模式:工厂模式例题

例题:


//造父类
class YunSuan
{
public $a;
public $b;
function YunSuan()
{

}
}
//造加法的子类
class Jia extends YunSuan
{
function YunSuan()
{
return($this->a + $this->b);
}
}
//造减法的子类
class Jian extends YunSuan
{
function YunSuan()
{
return ($this->a - $this->b);
}
}
//造乘法的子类
class Cheng extends YunSuan
{
function YunSuan()
{
return ($this->a * $this->b);
}
}
//造除法的子类
class Chu extends YunSuan
{
function YunSuan()
{
return ($this->a / $this->b);
}
}

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

ぉ 辰 プ 辰 ペ

原文地址:https://www.cnblogs.com/chenchen0815/p/5569316.html