Correction status:qualified
Teacher's comments:
父类
<?php 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 }
点击 "运行实例" 按钮查看在线实例
子类
<?php 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; } }
点击 "运行实例" 按钮查看在线实例
页面引用
<?php spl_autoload_register(function($class){ require 'class/'.$class.'.php'; }); $length = 5; $width = 6; $height = 10; $sq = new Square($length,$width,$height); echo '面积:'.$sq->ares().'<br>'; echo '周长:'.$sq->perimeter().'<br>'; echo '体积:'.$sq->volume();
点击 "运行实例" 按钮查看在线实例