這個例子可能并不實用,但基本概括了面向對象的三個特征:繼承性,封裝性,多態性。本例的主要功能有:
效果如下:

思路:
需要改進的地方:
index.php代碼如下:
1 <html> 2 <head> 3 <meta http-equiv="charset" content="utf-8"> 4 </head> 5 <body> 6 <div id="center"> 7 <h1>圖形周長面積計算器</h1> 8 <!--點擊鏈接的時候使用GET傳遞圖形的形狀屬性給index.php,也就是頁面本身--> 9 <a href="index.php?shape=rect">矩形</a>10 <a href="index.php?shape=triangle">三角形</a>11 <a href="index.php?shape=circle">圓形</a>12 </div>13 <div id="inputForm">14 <?php15 /*自動加載類*/16 function __autoload($className){17 include (strtolower($className).'.class.php');18 }19 20 /*21 1.先new一個Form對象,發現沒有form類的定義,把類名Form傳遞到自動加載類的函數參數進行類的自動加載。22 2.echo一個對象的引用,會調用該對象的__toString方法返回一個字符串,echo輸出的就是對象返回的字符串,23 這里輸出一個表單等待用戶的輸入。24 */25 echo new Form("index.php");26 27 /*如果用戶點擊了提交按鈕,自動加載result類,輸出結果*/28 if(isset($_POST["sub"])){29 echo new result();30 }31 ?>32 </div>33 </body>34 </html>form.class.php代碼如下:
1 <?php 2 /* 3 PRoject:面向對象版圖形計算器 4 file:form.class.php 5 description:對不同的圖形輸出不同的表單 6 */ 7 class form{ 8 private $formAction=NULL; //保存響應表單的文件 9 private $shape=NULL; //保存圖形的形狀10 11 /*12 @param string $action 對象初始化傳入的參數,代表響應的頁面的是哪一個文件13 */14 function __construct($action = ""){15 $this->formAction = $action; //把傳入的參數保存到$formAction中;16 $this->shape = isset($_GET["shape"]) ? $_GET["shape"]:"rect"; //從表單傳遞的變量中獲取圖形類別,如沒有傳遞,默認為矩形17 }18 function __toString(){19 $form = '<form action="'.$this->formAction.'?shape='.$this->shape.'" method="post">';20 //下面兩行使用變量函數調用對應圖形的私有函數,返回input部分表單的字符串21 $shape = 'get'.ucfirst($this->shape);22 $form .= $this->$shape();23 24 $form .= '</br><input type="submit" name="sub" value="計算"/></br>';25 $form .= '</form>';26 27 return $form;28 }29 //私有方法,返回矩形表單input部分的字符串;30 private function getRect(){31 //在表單提交后輸入的內容繼續顯示,防止其消失32 $formheight=isset($_POST['height']) ? $_POST['height'] : NULL;33 $formwidth=isset($_POST['width']) ? $_POST['width'] : NULL;34 $input = '<p>請輸入矩形的長和寬</p>';35 $input .= '矩形的高度:<input type="text" name="height" value="'.$formheight.'"/><br></br>';36 $input .= '矩形的寬度:<input type="text" name="width" value="'.$formwidth.'"/></br>';37 return $input;38 }39 //返回三角形輸入表單input部分的字符串40 private function getTriangle(){41 //在表單提交后繼續顯示出來,防止其消失42 $formside1=isset($_POST['side1']) ? $_POST['side1'] : NULL;43 $formside2=isset($_POST['side2']) ? $_POST['side2'] : NULL;44 $formside3=isset($_POST['side3']) ? $_POST['side3'] : NULL;45 $input = '<p>請輸入三角形的三邊</p>';46 $input .= '邊長1:<input type="text" name="side1" value="'.$formside1.'" /></br></br>';47 $input .= '邊長2:<input type="text" name="side2" value="'.$formside2.'"/></br></br>';48 $input .= '邊長3:<input type="text" name="side3" value="'.$formside3.'"/></br>';49 return $input;50 }51 //返回圓形表單input部分的字符串52 private function getCircle(){53 $formradius=isset($_POST['radius']) ? $_POST['radius'] : NULL; //在輸入表單提交后內容繼續顯示出來,防止其消失54 $input = '<p>請輸入半徑</p>';55 $input .= '半徑:<input type="text" name="radius" value="'.$formradius.'"/></br>';56 return $input;57 }58 }59
result.class.php代碼如下:
1 <?php 2 class result{ 3 private $shape = NULL; 4 5 //使用GET傳遞的變量,實例化一個相應的對象,返回一個對象的引用; 6 function __construct(){ 7 $this->shape = new $_GET["shape"](); 8 } 9 //調用對象的屬性和方法,返回周長和面積10 function __toString(){11 $result = $this->shape->shapeName.'的周長為'.$this->shape->perimeter().'</br>';12 $result .= $this->shape->shapeName.'的面積為'.$this->shape->area().'</br>';13 return $result;14 }15 } 抽象類shape.class.php代碼如下:
1 <?php 2 /* 3 project:面向對象版圖形計算器 4 file:shape.class.php 5 description:抽象類,定義兩個抽象方法area()和perimeter(),以及定義方法validate對輸入的值進行驗證 6 */ 7 abstract class shape{ 8 public $shapeName; //形狀名稱; 9 abstract function area(); //抽象類area(),讓子類去實現,體現多態性10 abstract function perimeter(); //抽象類perimeter();11 12 /*13 @param mixed $value 接受表單輸入值14 @param string $message 提示消息前綴15 @param boolean 返回值,成功為TRUE,失敗為FALSE16 */17 protected function validate($value,$message = "輸入的值"){18 if($value < 0 || $value == NULL || !is_numeric($value)){19 $message = $this->shapeName.$message;20 echo '<font color="red">'.$message.'必須為正數</font><br>';21 return FALSE;22 }23 else24 return TRUE;25 }26 }子類triangle.class.php代碼如下:
1 <?php 2 /** 3 project:面向對象版圖形計算器 4 file:triangle.class.php 5 description:繼承抽象類shape,計算并返回三角形的周長和面積 6 */ 7 class triangle extends shape{ 8 private $side1 = 0; //邊長1; 9 private $side2 = 0; //邊長2;10 private $side3 = 0; //邊長3;11 12 /*13 構造函數:對表單變量進行合理性驗證,通過則初始化三個邊長14 */15 function __construct(){16 $this->shapeName = "三角形"; //命名圖形17 18 //使用父類的方法validate檢查輸入的是否為正數19 if($this->validate($_POST["side1"],"邊長1") & $this->validate($_POST["side2"],"邊長2") & $this->validate($_POST["side3"],"邊長3")){20 21 //使用私有方法驗證兩邊和是否大于第三邊22 if($this->validatesum($_POST["side1"],$_POST["side2"],$_POST["side3"])){23 $this->side1 = $_POST["side1"]; //若通過驗證初始化三邊;24 $this->side2 = $_POST["side2"];25 $this->side3 = $_POST["sid
新聞熱點
疑難解答