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

 

 

ぉ 辰 プ 辰 ペ

posted @ 2016-06-08 10:42  辰辰0815  阅读(173)  评论(0编辑  收藏  举报