日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

前端学PHP之面向对象系列第六篇——简单图形面积计算器实现

發布時間:2023/12/15 php 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 前端学PHP之面向对象系列第六篇——简单图形面积计算器实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面的話

  本文用面向對象的技術來實現一個簡單的圖形面積計算器

?

圖形類

//rect.class.php <?phpabstract 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> <?phperror_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之面向对象系列第六篇——简单图形面积计算器实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。