图形类//rect.class.phpphp abstract class shape{ public $name; abstract function area(); abstract function view(); abstract function test($arr); }?>
主界面//index.phpdocumentclass=box> 图形计算器 矩形 三角形
php error_reporting(e_all & ~e_notice); function __autoload($classname){ include strtolower($classname)..class.php; } if(!empty($_get['action'])) { $classname = ucfirst($_get['action']); $shape=new $classname($_post); $shape->view(); if(isset($_post['dosubmit'])) { if($shape->test($_post)) { echo $shape->name.的面积为:.$shape->area().
; } } }else{ echo 请选择一个要计算的图形!
; } ?>
矩形类//rect.class.phpphpclass rect extends shape{ private $width; private $height; function __construct($arr=[]){ if(!empty($arr)){ $this->width = $arr['width']; $this->height = $arr['height']; } $this->name = 矩形; } function area() { return $this->width * $this->height; } function view() { $form = '
'; $form .=$this->name.'的宽:
'; $form .=$this->name.'的高:
'; $form .='
'; $form .=''; echo $form; } function test($arr) { $bg = true; if($arr['width'] ) { echo $this->name.的宽不能小于0!
; $bg = false; } if($arr['height'] ) { echo $this->name.的高度不能小于0!
; $bg = false; } return $bg; }}?>
三角形类//triangle.class.phpphpclass triangle extends shape{ private $b1; private $b2; private $b3; function __construct($arr=[]){ if(!empty($arr)){ $this->b1 = $arr['b1']; $this->b2 = $arr['b2']; $this->b3 = $arr['b3']; } $this->name = 三角形; } function area() { $p = ($this->b1 + $this->b2 + $this->b3)/2; return sqrt($p*($p-$this->b1)*($p-$this->b2)*($p-$this->b3)); } function view() { $form = '
'; $form .=$this->name.'第一个边的宽:
'; $form .=$this->name.'第二个边的宽:
'; $form .=$this->name.'第三个边的宽:
'; $form .='
'; $form .=''; echo $form; } function test($arr) { $bg = true; if($arr['b1'] ) { echo 第一个边的宽不能小于0!
; $bg = false; } if($arr['b2'] ) { echo 第二个边的宽不能小于0!
; $bg = false; } if($arr['b3'] ) { echo 第三个边的宽不能小于0!
; $bg = false; } if(($arr['b1'] + $arr['b2'] $arr['b3'])||($arr['b1'] + $arr['b3'] $arr['b2'])||($arr['b3'] + $arr['b2'] $arr['b1'])){ echo '两边之和不能小于第三边
'; $bg = false; } return $bg; }}?>
