父类
实例
class Area {
// 长
protected $legthNum = 0;
// 宽
protected $widthNum = 0;
// 常量 π
const pai = 3.14;
// 构造方法
public function __construct($lengthNum=0,$widthNum=0)
{
$this->legthNum = $lengthNum;
$this->widthNum = $widthNum;
}
// 面积
public function ares()
{
}
// 周长
public function perimeter()
{
}
//put your code here
}
运行实例 »
点击 "运行实例" 按钮查看在线实例
子类
实例
require 'Area.php';
//长方形/长方体
class Square extends Area{
//put your code here
//属性长方体-- 高
protected $heigthNum=0;
public function __construct($lengthNum = 0, $widthNum = 0,$heigthNum=0)
{
$this->legthNum=$lengthNum;
$this->widthNum=$widthNum;
$this->heigthNum=$heigthNum;
}
// 面积计算 方法重载
public function ares()
{
$squareAre = $this->legthNum*$this->widthNum;
return $squareAre;
}
// 周长计算 方法重载
public function perimeter()
{
$squarePerimeter = ($this->legthNum + $this->widthNum)*2;
return $squarePerimeter;
}
// 体积计算
public function volume(){
$squareVolume = $this->legthNum*$this->widthNum*$this->heigthNum;
return $squareVolume;
}
}
运行实例 »
点击 "运行实例" 按钮查看在线实例
页面引用
实例
spl_autoload_register(function($class){
require 'class/'.$class.'.php';
});
$length = 5;
$width = 6;
$height = 10;
$sq = new Square($length,$width,$height);
echo '面积:'.$sq->ares().'
';
echo '周长:'.$sq->perimeter().'
';
echo '体积:'.$sq->volume();
运行实例 »
点击 "运行实例" 按钮查看在线实例