有趣的php面向对象例子----带GPS全球定位系统,油表,里程表的小汽车
2010-10-30 20:40
218 查看
前两天看到一个一个挺有趣的php面向对象实例,讲解php面向对象挺有意思
原作者没编完留了一部分,觉得挺好玩,特意完善了一下,看代码:
这是原来作者写的,我修改了一部分,加强了”GPS“的显示 呵呵~~
里边少了Position 这个类,是作者留下给读者练手的,以下给出我的写法,新手写的不是很规范
原地址:http://www.showfan.cn/share.php?pid=835
原作者没编完留了一部分,觉得挺好玩,特意完善了一下,看代码:
class Car { /** * 小车的汽油量 * *@var *@access */ var $gas; /** * 里程记录 * *@var *@access */ var $meter; /** * 车的位置(由GPS自动控制) * *@var Object position *@access private */ var $position; /** * 发动机每1公里耗油量,这个车是0.1升 * *@var Integer *@access private */ var $engine=1; /** * 警报信息 * *@var *@access */ var $warning; /** 小车的初始化。新车出场当然要 1、加满汽油。 2、里程表归零。 3、清除警报信息。 4、设定出发位置。 */ public $farfrom; /** 距离目的地还有多远 */ public $alfar; /** 已经走了多远 */ function Car($gas,&$position) { $this->gas= intval($gas); //加满汽油 $this->meter = 0; $this->warning =''; //清除警报信息 $this->position = $position; //设定初始位置 $this->farfrom = 0;//初始化距离目的地 $this->alfar = 0;//初始化路程 } function getWarning() //返回警报信息 { return $this->warning; } function getGas() //返回汽油表指数 { return $this->gas; } function &getPosition() { return $this->position; //返回当前小车的位置 } function setHeading($direction='e') { $this->position->setDirection($direction); //因为使用了Position 对象,小汽车不需要自己来操心XY坐标值了,交给 Position 对象吧。 } /** * 开动小汽车 *@access public *@param INT 公里数 */ extends function run($km) { $goodRunFlag = true;//是否成功完成任务。 $maxDistance = $this->gas/$this->engine; //小车能够跑的最大距离。 if(($maxDistance)<$km) { $this->warning = '没有汽油了!';//设定警告信息,能跑多远就跑多远吧。 $goodRunFlag = false;//但是任务肯定完成不了。 $this->farfrom = $km - $maxDistance; $this->alfar = $maxDistance; } else { $maxDistance=$km; //没有问题,完成任务以后就可以停下来休息了。 } $this->position->move($maxDistance); //在坐标上移动由Position对象来完成,小汽车只要负责自己的油耗和公里表 就可以了。 $this->gas -= $maxDistance*$this->engine;//消耗汽油 $this->meter += $maxDistance; //增加公里表计数 return $goodRunFlag; } }
$startPoint = & new Position(3,9); //初始一个出发点坐标x=3,y=9 $myCar = & new Car(234,$startPoint); //我得到一个新的小车,新车初始燃油 500 升,出发地点$startPoint。 $myCar->setHeading('n'); //给小车设定方向 s:南方 n:北方 w:西方 e:东方。 if($myCar->run(234)) //然后让小车跑100公里,如果顺利完成任务显示燃油量。如果半途而废,我们显示警报信息。 { print('<br><b>小车一切正常,目前还有燃油:'.$myCar->getGas().'</b>');//获得燃油数 } else { print('<br><b>小车出问题了: '.$myCar->getWarning().'</b>');//显示警报信息 echo "<br />已经行驶了".$myCar->alfar."公里"; echo "<br />距离目的地还有".$myCar->farfrom."公里"; } $myPosition=$myCar->getPosition();//获得小车当前的位置 print('<br>我的小车现在<br> X:'.$myPosition->getX().'Y:'.$myPosition->getY());//显示小车的坐标位置
这是原来作者写的,我修改了一部分,加强了”GPS“的显示 呵呵~~
里边少了Position 这个类,是作者留下给读者练手的,以下给出我的写法,新手写的不是很规范
class Position{ public $cdx;//原始x坐标 public $cdy;//原始y坐标 public $direction;//输入方向 public $newx;//移动后新X坐标 public $newy;//移动后新Y坐标 function __construct($x, $y){ $this->cdx = $x;//GPS接受原始xy坐标 $this->cdy = $y; } function move($maxDistance){//我们的小车在欢快的跑了。。 switch($this->direction){ case 'e' : $this->newx = $this->cdx + $maxDistance; $this->newy = $this->cdy;break; case 'w' : $this->newx = $this->cdx - $maxDistance; $this->newy = $this->cdy;break; case 's' : $this->newy = $this->cdy - $maxDistance; $this->newx = $this->cdx;break; case 'n' : $this->newy = $this->cdy + $maxDistance; $this->newx = $this->cdx;break; } } //GPS接受前进方向 function setDirection($direction){ $this->direction = $direction; } //告诉雷达到什么位置了 X坐标 function getX(){ return $this->newx; } //同理,Y坐标 function getY(){ return $this->newy; } }
原地址:http://www.showfan.cn/share.php?pid=835
相关文章推荐
- php面向对象小例子
- PHP面向对象之旅:一个接口实例,很好的例子
- PHP面向对象之旅:一个接口实例,很好的例子
- php面向对象全攻略 (二) 实例化对象 使用对象成员
- PHP中用mysqli面向对象打开连接关闭mysql数据库的方法
- PHP 面向对象
- PHP面向对象之我见
- PHP面向对象概念
- PHP面向对象中this,self,parent区别和使用
- PHP面向对象之旅:构造函数
- 深入PHP面向对象、模式与实践——对象
- PHP面向对象之旅:接口与组合模拟多继承
- Java学习第9天(5):面向对象-多态-应用例子
- php 面向对象 中的self
- PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- php面向对象中的魔术方法
- php代码之面向对象基础一
- php 面向对象和面向过程区别
- php面向对象不同类型成员访问方法 小结