php 工厂模式
2016-06-12 16:07
260 查看
<body> <?php //设计模式:工厂模式 /* class YunSuan { public $a; public $b; 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 Jian(); $y->a = 10; $y->b = 5; //echo $y->YunSuan(); //再优化,工厂类 class GongChang { static function DuiXiang($f) { switch($f) { case "+": return new Jia(); break; case "-": return new Jian(); break; case "*": return new Cheng(); break; } } } $r = GongChang::DuiXiang("-"); $r->a=10; $r->b = 5; echo $r->YunSuan(); /* class Suan { public $total; function Jia($a) { $this->total = $this->total+$a; return $this->total; } function Jian($a) { $this->total = $this->total-$a; } }*/ ?> </body> </html>
View Code
相关文章推荐
- php 测试异步回调是否执行
- PHP生成制作验证码的简单实例
- php 如何转义emoji表情,让它可以存入utf8的数据库
- ViewPager中的FragmentPagerAdapter,FragmentStatePagerAdapter
- Yii 视图渲染过程
- DotProject首页、文档和下载 - 项目管理工具 - 开源中国社区
- thinkphp 查看sql语句
- yii2框架-yii2的asset资源包(十一)
- Yii2 手动创建新应用
- MySQL和PHP基础考试错题回顾
- PHPExcel读取excel文件示例
- 当NAT遇到PPTP
- Yii web创建过程
- php测试题
- vsftpd
- ThinkPHP URL路由解析
- php gd等比例缩放压缩图片函数
- php下的命令行执行
- PHP通过header和meta实现页面编码声明
- PHP 单元测试(PHPUnit)