♂WhiteWin♂

导航

【辅导】Task12 类的定义与继承练习

<?php
// 类文件名为Cylinder.class.php,放在classes文件夹下
// 从Circle类继承
class Cylinder extends Circle{
   // 定义扩展字段
   private $hegiht;
   
   // 封装成读属性
   public function getHeight(){
       return $this->hegiht;
   }
   
   // 封装成写属性
   public function setHeight($h){
       if ($h>0.0){
           $this->hegiht = $h;
       }
   }
   
   // 重载构造函数
   function __construct($h,$r,$x=0,$y=0){
       // 调用父类构造函数初始化
       parent::__construct($r,$x,$y);
       // 给本类成员初始化
       $this->setHeight($h);
   }
   
   // 重载求面积方法
   function getArea(){
       return 2.0*parent::getArea()
         + parent::getDiameter()*$this->getHeight();
   }
   
   // 添加求体积的方法
   function getVolume(){
       return parent::getArea()*$this->getHeight();
   }
   
   // 重载toString方法
   function __toString(){
       return "圆柱体的底圆".parent::__toString()
       .",高度={$this->height}";
   }
}

 

posted on 2022-04-07 08:43  银翼  阅读(31)  评论(0编辑  收藏  举报