剛剛學完phphtml' target='_blank'>面向對象的編程,參考著高洛峰老師的php教程學習了這個實例。
效果圖片:<html><head> <title>圖形計算(使用面向對象開發技術)</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body> <center> <h1>圖形(周長&面積) 計算器</h1> <a href="index.php?action=rect">矩形</a> <a href="index.php?action=triangle">三角形</a> <a href="index.php?action=circle">圓形</a> <hr> </center> <?php //php代碼部分 error_reporting(E_ALL & ~E_NOTICE);//提示錯誤的等級 // __autoload是php中的魔術方法,在用到類的時候自動調用 function __autoload($className){ //自動導入這個類 include strtolower($className).".class.php"; } //輸出表單,form類中有魔術方法__toString,因此可以直接輸出類的對象引用,就是輸出對象返回的字符串 echo new Form(); if(isset($_POST["sub"])){ //輸出結果 echo new Result();//直接輸出對象的引用表示 } ?></body></html>form.class.php_這是表單類_
<?php //根據index中提交的不同action的值返回不同的表單 class Form{ private $action; private $shape; //構造方法 function __construct($action=""){ $this->action = $action; $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect"; } function __toString() { // TODO: Implement __toString() method. $form='<form action="'.$this->action.'" method="post">'; switch($this->shape){ case "rect": $form.=$this->getRect(); break; case "triangle": $form.=$this->getTriangle(); break; case "circle": $form.=$this->getCircle(); break; default: $form.='請選擇一個形狀<br>'; } $form.='<input type="submit" name="sub" value="計算">'; $form.='</form>'; return $form; } private function getRect(){ $input='<b>請輸入 | 矩形 | 的寬度和高度:</b><p>'; $input.='寬度:<input type="text" name="width" value="'.$_POST['width'].'"><br>'; $input.='高度:<input type="text" name="height" value="'.$_POST['height'].'"><br>'; $input.='<input type="hidden" name="action" value="rect">'; return $input; } private function getTriangle(){ $input='<b>請輸入 | 三角形 | 的三條邊:</b><p>'; $input.='第一邊: <input type="text" name="side1" value="'.$_POST['side1'].'" ><br>'; $input.='第二邊: <input type="text" name="side2" value="'.$_POST['side2'].'" ><br>'; $input.='第三邊: <input type="text" name="side3" value="'.$_POST['side3'].'" ><br>'; $input.='<input type="hidden" name="action" value="triangle">'; return $input; } private function getCircle(){ $input='<b>請輸入 | 圓形 | 的半徑:</b><p>'; $input.='半徑: <input type="text" name="radius" value="'.$_POST['radius'].'" ><br>'; $input.='<input type="hidden" name="action" value="circle">'; return $input; } }/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:26 * */shape.class.php 這是一個抽象類,用來定義規范的
<?phpabstract class Shape { public $shapeName; //規范circle、triangle、rect中必須有area()、perimeter()方法 abstract function area(); abstract function perimeter(); public function setShapeName($shapeName) { $this->shapeName = $shapeName; return $this; } //判斷輸入的數字是否為大于0的有效數字 protected function validate($value, $message="形狀"){ if($value == "" || !is_numeric($value) || $value < 0 ){ echo '<font color="red"> '.$message.' 必須為非負值的數字,并且不能為空 </font><br>'; return false; } else { return true; } }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:42 */circle.class.php_就是計算周長和面積的公式了_
<?phpclass Circle extends Shape { private $radius=0; function __construct(){ $this->shapeName="圓形"; if($this->validate($_POST['radius'], '圓的半徑')){ $this->radius=$_POST["radius"]; }else{ exit; } } function area(){ return pi()*$this->radius*$this->radius; } function perimeter(){ return 2*pi()*$this->radius; }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:06 */rect.class.php
<?phpclass Rect extends Shape{ private $width=0; private $height=0; function __construct() { $this->shapeName="矩形"; if($this->validate($_POST["width"],'矩形的寬度') & $this->validate($_POST["height"],'矩形的高度')) { $this->width=$_POST["width"]; $this->height=$_POST["height"]; } else{ exit; } } function area(){ return $this->width*$this->height; } function perimeter() { return 2 * ($this->width + $this->height); }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:02 */triangle.class.php
<?phpclass Triangle extends Shape{ private $side1=0; private $side2=0; private $side3=0; function __construct(){ $this->shapeName="三角形"; if($this->validate($_POST['side1'], '三角形的第一個邊')){ $this->side1=$_POST["side1"]; } if($this->validate($_POST['side2'], '三角形的第二個邊')){ $this->side2=$_POST["side2"]; } if($this->validate($_POST['side3'], '三角形的第三個邊')){ $this->side3=$_POST["side3"]; } if(!$this->validateSum()){ echo '<font color="red">三角形的兩邊之和必須大于第三邊</font>'; exit; } } function area(){ $s=( $this->side1+$this->side2+$this->side3 )/2; return sqrt( $s * ($s - $this->side1) * ($s - $this->side2) * ($s - $this->side3) ); } function perimeter(){ return $this->side1+$this->side2+$this->side3; } private function validateSum() { $condition1 = ($this->side1 + $this->side2) > $this->side3; $condition2 = ($this->side1 + $this->side3) > $this->side2; $condition3 = ($this->side2 + $this->side3) > $this->side1; if ($condition1 && $condition2 && $condition3) { return true; } else { return false; } }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:04 */result.class.php_這里是返回計算結果類_
<?php class Result{ private $shape; function __construct() { switch($_POST['action']){ case 'rect': $this->shape=new Rect(); break; case 'triangle': $this->shape=new Triangle(); break; case 'circle': $this->shape=new Circle(); break; default: $this->shape=false; } } /** * @return string */ function __toString() { // TODO: Implement __toString() method. if($this->shape){ $result=$this->shape->shapeName.'的周長:'.$this->shape->perimeter().'<br>'; $result.=$this->shape->shapeName.'的面積:'.$this->shape->area().'<br>'; return $result; }else{ return '沒有這個形狀'; } } }/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:47 */
相關推薦:
PHP面向對象 靜態延遲綁定static::
以上就是php面向對象編程練習:計算矩形、三角形、圓形的周長和面積 的詳細內容,更多請關注 其它相關文章!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答