本文用面向对象的技术来实现一个简单的图形面积计算器
//rect.class.php php abstract class Shape{ public $name; abstract function area(); abstract function view(); abstract function test($arr); } ?>
//index.phpDocument 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.php php class 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 = ''; echo $form; } function test($arr) { $bg = true; if($arr['width'] < 0) { echo $this->name."的宽不能小于0!
//triangle.class.php php class 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 = ''; echo $form; } function test($arr) { $bg = true; if($arr['b1'] < 0) { echo "第一个边的宽不能小于0!
已抢111236个
抢已抢47108个
抢已抢133824个
抢已抢32401个
抢已抢19628个
抢