/*//设计模式:程序进行设计用的,工厂模式
class YunSuan { public $a; public $b; public $f; function Jia() { return ($this->a+$this->b); } function Jian() { return ($this->a-$this->b); } function Cheng() { return ($this->a*$this->b); } function Chu() { return ($this->a/$this->b); } function Yu() { return ($this->a%$this->b); } }
$y = new YunSuan(); $y->a=10; $y->b=5; echo $y->Jia();*/ //造父类,用子类继承 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); } }
$y = new Jia(); $y->a=10; $y->b=5; echo $y->YunSuan();
//再优化,工厂类 class GongChang { public static $f; static function DuiXiang($f) { switch($f) { case "+": return new Jia(); break; case"-": return new Jian(); break; case"*": return new Cheng(); break; } } } $r =GongChang::DuiXiang("+"); $y->a=10; $y->b=5; echo $y->YunSuan();