前端学PHP之面向对象系列第六篇——简单图形面积计算器实现
2016-11-15 16:30
489 查看
前面的话
本文用面向对象的技术来实现一个简单的图形面积计算器图形类
//rect.class.php <?php abstract class Shape{ public $name; abstract function area(); abstract function view(); abstract function test($arr); } ?>
主界面
//index.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ width: 800px; margin: 0 auto; } </style> </head> <body> <div class="box"> <h1>图形计算器</h1> <div> <a href="index.php?action=rect">矩形</a> <a href="index.php?action=triangle">三角形</a> </div> </div> <?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()."<br>"; } } }else{ echo "请选择一个要计算的图形!<br>"; } ?> </body> </html>
矩形类
//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 = '<form action="index.php?action=rect" method="post">'; $form .=$this->name.'的宽:<input name="width" value=""/><br>'; $form .=$this->name.'的高:<input name="height" value=""/><br>'; $form .='<input type="submit" name="dosubmit" value="计算"><br>'; $form .='</form>'; echo $form; } function test($arr) { $bg = true; if($arr['width'] < 0) { echo $this->name."的宽不能小于0!<br>"; $bg = false; } if($arr['height'] < 0) { echo $this->name."的高度不能小于0!<br>"; $bg = false; } return $bg; } } ?>
三角形类
//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 = '<form action="index.php?action=triangle" method="post">'; $form .=$this->name.'第一个边的宽:<input name="b1" value=""/><br>'; $form .=$this->name.'第二个边的宽:<input name="b2" value=""/><br>'; $form .=$this->name.'第三个边的宽:<input name="b3" value=""/><br>'; $form .='<input type="submit" name="dosubmit" value="计算"><br>'; $form .='</form>'; echo $form; } function test($arr) { $bg = true; if($arr['b1'] < 0) { echo "第一个边的宽不能小于0!<br>"; $bg = false; } if($arr['b2'] < 0) { echo "第二个边的宽不能小于0!<br>"; $bg = false; } if($arr['b3'] < 0) { echo "第三个边的宽不能小于0!<br>"; $bg = false; } if(($arr['b1'] + $arr['b2'] < $arr['b3'])||($arr['b1'] + $arr['b3'] < $arr['b2'])||($arr['b3'] + $arr['b2'] < $arr['b1'])){ echo '两边之和不能小于第三边<br>'; $bg = false; } return $bg; } } ?>
相关文章推荐
- 用php简单实现加减乘除计算器
- php实现一个简单的四则运算计算器
- php实现简单的计算器
- 用php简单实现加减乘除计算器
- PHP实现一个简单的计算器(在html页面和php文件之间传值)
- 用php简单实现加减乘除计算器
- php:的图形计算器的面向对象的版本武器2
- 前端学PHP之面向对象系列第五篇——对象操作
- 前端学PHP之面向对象系列第一篇——类和对象
- PHP实现的简单四则运算计算器功能示例
- 利用php代码实现网页版简单的计算器功能
- php面向对象实现简单的用户注册登陆
- 前端学PHP之面向对象系列第三篇——三大特性
- 前端学PHP之面向对象系列第四篇——关键字
- 【前端】Vue.js实现简单价格计算器
- php编程实现简单的网页版计算器功能示例
- 前端学PHP之面向对象系列第二篇——魔术方法
- 从此不求人:自主研发一套PHP前端开发框架(30) 实现简单的用户登录功能
- 用php简单实现加减乘除计算器
- 用php简单实现小计算器