首页 > 代码库 > 面向对象求圆的阴影面积实例

面向对象求圆的阴影面积实例

面向对象例子  (求阴影部分面积)

技术分享

(1)面向过程

$r1 = 10;

$r2 = 5;

$mj = 3.14*$r1*$r1-3.14*$r2*$r2;

 

echo $mj;

 

(2)面向对象

class Yuan

{

       public $r; 半径

       function __construct($bj) 构造函数

       {

              $this->r = $bj;

       }

      

       function mianJi() 返回圆的面积

       {

              return 3.14*$this->r*$this->r;

       }

}

$y1 = new Yuan(10);

$y2 = new Yuan(5);

$zmj = $y1->mianJi()-$y2->mianJi();

echo $zmj;

面向对象求圆的阴影面积实例