有趣的php面向对象例子----带GPS全球定位系统,油表,里程表的小汽车

前两天看到一个一个挺有趣的php面向对象实例,讲解php面向对象挺有意思

原作者没编完留了一部分,觉得挺好玩,特意完善了一下,看代码:

 
  
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

转载于:https://www.cnblogs.com/banruo/archive/2010/10/30/1865257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值