使用面向对象的图形计算器,面向对象图形计算器_PHP教程

WBOY
Release: 2016-07-13 09:50:47
Original
1149 people have browsed it

使用面向对象的图形计算器,面向对象图形计算器

这个例子可能并不实用,但基本概括了面向对象的三个特征:继承性,封装性,多态性。本例的主要功能有:

效果如下:

思路:

需要改进的地方:

index.php代码如下:

 1   2   3   4 
  5   6 
7

图形周长面积计算器

8 9 矩形 10 三角形 11 圆形 12
13
14 php 15 /*自动加载类*/ 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
33 34
Copy after login

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 = '
'; 20 //下面两行使用变量函数调用对应图形的私有函数,返回input部分表单的字符串 21 $shape = 'get'.ucfirst($this->shape); 22 $form .= $this->$shape(); 23 24 $form .= '

'; 25 $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 = '

请输入矩形的长和宽

'; 35 $input .= '矩形的高度:

'; 36 $input .= '矩形的宽度:
'; 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 = '

请输入三角形的三边

'; 46 $input .= '边长1:

'; 47 $input .= '边长2:

'; 48 $input .= '边长3:
'; 49 return $input; 50 } 51 //返回圆形表单input部分的字符串 52 private function getCircle(){ 53 $formradius=isset($_POST['radius']) ? $_POST['radius'] : NULL; //在输入表单提交后内容继续显示出来,防止其消失 54 $input = '

请输入半径

'; 55 $input .= '半径:
'; 56 return $input; 57 } 58 } 59
Copy after login

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().'
'; 12 $result .= $this->shape->shapeName.'的面积为'.$this->shape->area().'
'; 13 return $result; 14 } 15 }
Copy after login

抽象类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,失败为FALSE 16 */ 17 protected function validate($value,$message = "输入的值"){ 18 if($value < 0 || $value == NULL || !is_numeric($value)){ 19 $message = $this->shapeName.$message; 20 echo ''.$message.'必须为正数
'; 21 return FALSE; 22 } 23 else 24 return TRUE; 25 } 26 }
Copy after login

子类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["side3"]; 26  } 27 else{ 28 echo '两边的和要大于第三边'; 29 exit(); 30  } 31  } 32 else{ 33 exit(); 34  } 35  } 36 /*使用海伦公式计算面积,并返回结果*/ 37 function area(){ 38 $s = ($_POST["side1"] + $_POST["side2"] + $_POST["side3"])/2; 39 return sqrt($s * ($s - $_POST["side1"]) * ($s - $_POST["side2"]) * ($s - $_POST["side3"])); 40  } 41 /*计算并返回周长*/ 42 function perimeter(){ 43 return $_POST["side1"] + $_POST["side2"] + $_POST["side3"]; 44  } 45 /*计算三角形两边和是否大于第三边,是返回TRUE,否返回FALSE*/ 46 private function validatesum($side1,$side2,$side3){ 47 if(($side1 + $side2) > $side3 && ($side1 + $side3) > $side2 && ($side2 + $side3) > $side1) 48 return TRUE; 49 else 50 return FALSE; 51  } 52 }
Copy after login

子类circle.class.php代码如下:

 1 php  2 /*  3  project:面向对象的图形计算器  4  file:circle.class.php  5  description:接收表单值,返回周长和面积  6 */  7 class circle extends shape{  8 private $radius; //圆的半径  9 10  //初始化圆的名称,检查输入合法性并初始化半径 11 function __construct(){ 12 $this->shapeName = "圆形"; 13 if($this->validate($_POST["radius"],"半径")) 14 $this->radius = $_POST["radius"]; 15  } 16 //返回圆的面积 17 function area(){ 18 return 3.14 * $this->radius * $this->radius; 19  } 20 //返回圆的周长 21 function perimeter(){ 22 return 3.14 * 2 * $this->radius; 23  } 24 }
Copy after login

子类rect.class.php代码如下:

 1 php  2 /*  3  project:面向对象的图形计算器  4  file:rect.class.php  5  descrition:声明一个矩形资料,实现形状抽象类计算周长和面积的方法,返回矩形的周长和面积  6 */  7 class rect extends shape{  8 private $width; //矩形的宽度  9 private $height; //矩形的高度 10 11  //使用父类的validate方法验证输入的合法性,通过则初始化宽度和高度 12 function __construct(){ 13 $this->shapeName = "矩形"; 14 if($this->validate($_POST["width"],"宽度") && $this->validate($_POST["height"],"高度")){ 15 $this->width = $_POST["width"]; 16 $this->height = $_POST["height"]; 17  } 18  } 19 //返回面积 20 function area(){ 21 return $this->width * $this->height; 22  } 23 //返回周长 24 function perimeter(){ 25 return 2 * ($this->width + $this->height); 26  } 27 }
Copy after login

声明:

  1.本文只适合实验,不适合现实应用,若造成不良后果,本人概不负责。

  2.本文为原创博客,可以在个人平台自由转载,但需要注明出处,附上链接,否则视为盗用。严禁用于商业用途,如有需要,联系本人支付稿费,授权后方能使用。

www.bkjia.com true http://www.bkjia.com/PHPjc/1017048.html TechArticle 使用面向对象的图形计算器,面向对象图形计算器 这个例子可能并不实用,但基本概括了面向对象的三个特征:继承性,封装性,多态性。...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn