php之类,对象(四)加载类及练习题
一、加载类:
1.命名类文件的时候每个单词首字母大写,后面缀上.class.php
eg: Info.class.php
在写编码时定义类名首字母大写,定义变量名小写
eg:class Ren
{
public $name;
public $sex;
}
2.加载类:
2.1 如何找到类的文件
//当前目录:./
//上级目录:../
//下级目录:目录名/
//根目录:/
//如果实在php代码里面/代表本地磁盘的跟
//如果是在html里面/代表当前站点目录
include("../Info.class.php");//将文件加载到页面,参数是一个路径(能找到类文件)
include "/Appserve/www/Info.class.php";
require_once "./Info.class.php";//请求目标文件一次
require_once ("./Info.class.php");
require("Info.class.php");
require "Info.class.php";
2.2自动加载类:
function __autoload($classname) //括号里是类名
{
include ($classname.".class.php");
}
要求:1.所有类的命名规则要一样 2.所有的类要在同一个文件夹下 3.类的命名最好和文件名一致
附:一般不太用这个方法
二、例子:
1.求大圆和小圆的中间阴影面积:
//面向过程:
$r1=10;
$r2=5;
$mj=$r1*$r1*3.14-$r2*$r2*3.14;
echo $mj;
//面向对象:
(1.)新建一个php文件,命名为 Yuan.class.php
class Yuan
{
public $r;
function __construct($r)
{
$this->r=$r;
}
functin MianJi()
{
return $this->r*$this->r*3.14;
}
}
(2.)源代码:
include ("Yuan.class.php");
//include "Yuan.class.php";
//require_once ("Yuan.class.php");
//require_once "Yuan.class.php";
//require "Yuan.class.php";
//require ("Yuan.class.php");
$y1=new Yuan(10);
$y2=new Yuan(5);
echo $y1->MianincluJi()-$y2->MianJi();
2.用面向对象的方法把一张Info表的数据显示在页面上
//显示数据:
附:将表和要显示的数据联系起来时,
//表和类对应:表名是类名,列名是类里面的成员
//表里面的每一条数据对应的是类实例化的对象
做法:
1.新建一个文件,做一个Info表的类,表名为Info.class.php,里面是纯php代码
<?php
//Info表的实体类
class Info
{
public $code;
public $name;
public $sex;
public $nation;
public $birtday;
}
2.在源代码中引用这个类:
include "Info.class.php";
//开始制作Info表内的数据:
$arr=array();
$info1=new Info();
$info1->code="p001";
$info1->name="张三";
$info1->sex="男";
$info1->nation="汉族";
$info1->birthday="1977-03-13";
//往数组里面追加元素,把对象放到数组里用array_push()
array_push($arr,$info1);
$info2=new Info();
$info2->code="p002";
$info2->name="李四";
$info2->sex="女";
$info2->nation="回族";
$info2->birthday="1988-03-13";
array_push($arr,$info2);
$info3=new Info();
$info3->code="p003";
$info3->name="王武";
$info3->sex="男";
$info3->nation="苗族";
$info3->birthday="1983-03-13";
array_push($arr,$info3);
echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
echo "<tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";
foreach ($arr as $value)
{
echo "<tr>
<td>{$value->code}</td>
<td>{$value->name}</td>
<td>{$value->sex}</td>
<td>{$value->nation}</td>
<td>{$value->birthday}</td>
</tr>";
}
echo "</table>";
3. 做一个游戏Hero
<1.>新建一个Hero的php文件,命名为Hero.class.php
class Hero
{
public $name;
public $blood;
public $attact;
public $experience;
public $level;
public $money;
public $skill= array();
//构造函数,对成员进行初始化
function __construct($n)
{
$this->name = $n;
$this->blood = 100;
$this->attact = 10;
$this->experience=0;
$this->level=1;
$this->money = 100;
}
//打怪函数
function DaGuai()
{
//杀死怪物随机获取一个数值并取整数
$sj = floor(rand(0,100));
if($sj>30) //随机获取数值大于30增加经验值
{
//获取经验
$exp= floor(rand(0,40));
//将该英雄经验增加
$this->experience = $this->experience+$exp;
//判断是否要升级
if($this->experience>=50)
{
$this->level +=1;
$this->experience= 0;
$this->blood += 20;
$this->attact +=5;
}
echo $this->name."杀死了一个怪物,获得了{$exp}点经验<br>";
}
else
{
if($this->level==1)
{
}
else
{
$this->level -=1;
}
echo "你被怪物打死了<br>";
}
}
//查看英雄信息
function Show()
{
echo "英雄名称:{$this->name}<br>";
echo "英雄血量:{$this->blood}<br>";
echo "英雄攻击:{$this->attact}<br>";
echo "英雄经验:{$this->experience}<br>";
echo "英雄等级:{$this->level}<br>";
echo "技能为:";
foreach($this->skill as $v)
{
echo $v."!";
}
}
//学习技能
function XueXi()
{
//花钱
$hf = floor(rand(0,20));
$n = floor(rand(0,5)); //5种技能随机学习
//技能库里面选技能
switch($n)
{
case 1:
array_push($this->skill,"冲锋");
break;
case 2:
array_push($this->skill,"嘲讽");
break;
case 3:
array_push($this->skill,"致死打击");
break;
case 4:
array_push($this->skill,"盾墙");
break;
case 5:
array_push($this->skill,"沉默");
break;
}
}
}
<2.>源代码:
include("Hero.class.php");
$hero = new Hero("武松"); //创建英雄
$hero->DaGuai();
$hero->XueXi();
$hero->Show();