weixin_64113726 2022-03-11 19:57 采纳率: 33.3%
浏览 57
已结题

关于#php#的问题:PHP定义一个计算图形面积抽象类,定义两个子类分别用于实现计算矩形和椭圆形的面积分别实例化两个子类的对象用于计算指定高与宽图形的面积

PHP定义一个计算图形面积抽象类,定义两个子类分别用于实现计算矩形和椭圆形的面积分别实例化两个子类的对象用于计算指定高与宽图形的面积。

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-03-11 20:40
    关注

    代码如下

    img

    <meta charset="utf-8">
    <?php 
    abstract class Shape{
        public $width;
        public $height;
        abstract public function GetArea();
    }
    class Rectangle extends Shape{
        public function __construct($width, $height) {
            $this->width = $width;
            $this->height = $height;
        }
        function GetArea(){return $this->width * $this->height;}
    }
    class Ellipse extends Shape{
        
        public function __construct( $width, $height ) {
            $this->width = $width;
            $this->height = $height;
        }
        function GetArea(){return round(pi()*$this->width * $this->height,2);}
    }
     
    $rect=new Rectangle(3,4);
    echo "矩形面积:3x4=".$rect->GetArea().'<br>';
    $ell=new Ellipse(3,4);
    echo "椭圆面积:3x4=".$ell->GetArea().'<br>';
    
    ?>
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月19日
  • 已采纳回答 3月11日
  • 创建了问题 3月11日

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式