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

关于#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日