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

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

代码如下

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

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632