码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象(游戏2)

时间:2017-07-23 14:33:43      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:extend   created   打猎   end   php   bst   parent   pre   tor   

<?php
/**
* Created by PhpStorm.
* User: fu
* Date: 2017/7/21
* Time: 16:17
*/

/**
* 抽象的角色类,作为所有的角色的模板
* Class JueSe
*/
abstract class JueSe{
/** 角色的名字 */
protected $mingZi;
/** 角色的等级 */
protected $dengJi;
/** 角色的包裹 */
protected $baoGuo;
/** 角色的攻击力 */
protected $gongJiLi;
/** 角色的防御力 */
protected $fangYuLi;
/** 角色的生命值 */
protected $shengMingZhi;

/** 盗贼 */
function __construct($mingZi,$dengJi=0,$baoGuo=10,
$gongJiLi=1,$fangYuLi=1,$shengMingZhi=50)
{
$this->mingZi = $mingZi;
$this->dengJi = $dengJi;
$this->baoGuo = $baoGuo;
$this->gongJiLi = $gongJiLi;
$this->fangYuLi = $fangYuLi;
$this->shengMingZhi = $shengMingZhi;
}
/**
* 自己介绍自己的行为
*/
public function jieShaoZiJi(){
echo ‘我的角色是:‘.$this->mingZi.‘<br>‘.‘等级:‘.$this->dengJi.‘<br>‘.‘攻击力:‘.$this->gongJiLi.
‘<br>‘.‘血量:‘.$this->shengMingZhi.‘<br>‘.‘防御力:‘.$this->fangYuLi.‘<br>‘.‘包裹空间:‘.$this->baoGuo.‘<br>‘;
}
}

/**
* 盗贼可以偷盗
* Interface ITouDao
*/
interface ITouDao{
function touDao($jueSe);
}

/**
* 猎人可以打猎
* Interface IDaLie
*/
interface IDaLie{
function daLie($jueSe);
}

/**
* 农民可以采矿。
* Interface ICaiKuang
*/
interface ICaiKuang{
function caiKuang($jueSe);
}

/**
* 盗贼、猎人和农民可以攻击。
* Interface IGongJi
*/
interface IGongJi{
function gongJi($jueSe);
}

/**
* 农民、医生可以治疗。
* Interface IFangYu
*/
interface IFangYu{
function fangYu($jueSe);
}

/**
* 士兵可以抵抗盗贼、猎人和农民等的攻击。
* Interface IZhiLiao
*/
interface IZhiLiao{
function zhiLiao($jueSe);
}

/**
* 盗贼类
* Class DaoZei
*/
class DaoZei extends JueSe implements ITouDao, IGongJi {
/**
* 盗贼的偷盗的行为
* @param $jueSe 被偷的角色
*/
function touDao($jueSe){
echo ‘盗贼可以偷盗<br>‘;
}
/**
* 盗贼可以攻击
* @param $jueSe 被攻击的角色
*/
function gongJi($jueSe){
echo ‘盗贼可以攻击<br>‘;
}

public function jieShaoZiJi(){
parent::jieShaoZiJi();
$this->touDao(‘‘);
$this->gongJi(‘‘);
}
}

/**
* 玩家类
* Class WanJia
*/
class WanJia{
/** 玩家的名字 */
private $mingZi;

function __construct($mingZi)
{
$this->mingZi = $mingZi;
}

/**
* 玩家可以控制角色
* @param $jueSe 被玩家所控制的角色对象
*/
public function kongZhi($jueSe){
echo ‘我是玩家:‘.$this->mingZi.‘<br>‘;
$jueSe->jieShaoZiJi();
}
}

$wj = new WanJia(‘王鹏菲‘);
$jueSe = new DaoZei(‘盗贼‘);
$wj->kongZhi($jueSe);

面向对象(游戏2)

标签:extend   created   打猎   end   php   bst   parent   pre   tor   

原文地址:http://www.cnblogs.com/2833824328love/p/7224400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!