PHP语言入门-使用面向对象思想开发的图形计算器
小标 2018-07-12 来源 : 阅读 1608 评论 0

摘要:本文主要向大家介绍了PHP语言入门-使用面向对象思想开发的图形计算器,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言入门-使用面向对象思想开发的图形计算器,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文使用面向对象的思想,做了一个图形计算器的小例子:

1、架构图

 PHP语言入门-使用面向对象思想开发的图形计算器

2、说明

(1)index.php页面只需要直接调用Form类和Result类

(2)Form类用于输入一个表单

(3)为了实现多态的特性,Result类用于操作数据

3、Shape类

[php] view plain copy

1. <?php  

2.     abstract class Shape{  

3.         abstract function area();//面积  

4.         abstract function perimeter();//周长  

5.     }  

4、Rectangle类

[php] view plain copy

1. <?php  

2.     class Rectangle extends Shape {  

3.         private $width=0;  

4.         private $height=0;  

5.   

6.         function __construct($width,$height){  

7.             $this->width=$width;  

8.             $this->height=$height;  

9.         }  

10.   

11.         function area(){  

12.             return $this->width * $this->height;  

13.         }  

14.   

15.         function perimeter(){  

16.             return 2*$this->width + 2*$this->height;  

17.         }  

18. }  

5、Triangle类

[php] view plain copy

1. <?php  

2.     class Triangle extends Shape {  

3.         private $side1=0;  

4.         private $side2=0;  

5.         private $side3=0;  

6.   

7.         function __construct($side1,$side2,$side3){  

8.             $this->side1=$side1;  

9.             $this->side2=$side2;  

10.             $this->side3=$side3;  

11.         }  

12.   

13.         function area(){  

14.             if ($this->isTriangle()){  

15.                 $temp=($this->side1 + $this->side2 + $this->side3)/2;  

16.                 return sqrt($temp * ($temp - $this->side1) * ($temp - $this->side2) * ($temp - $this->side3) );  

17.             }else{  

18.                 return "无法构成三角形";  

19.             }  

20.         }  

21.   

22.         function perimeter(){  

23.             if ($this->isTriangle()){  

24.                 return $this->side1 + $this->side2 + $this->side3;  

25.             }else{  

26.                 return "无法构成三角形";  

27.             }  

28.         }  

29.   

30.         //需要验证是否能构成三角形  

31.         function isTriangle(){  

32.             if (($this->side1 + $this->side2 > $this->side3)&&($this->side1 + $this->side3 > $this->side2)&&($this->side2 + $this->side3 > $this->side1)){  

33.                 return true;  

34.             }else{  

35.                 return false;  

36.             }  

37.         }  

38. }  

6、Circle类

[php] view plain copy

1. <?php  

2.     class Circle extends Shape {  

3.         private $radius=0;  

4.   

5.         function __construct($radius){  

6.             $this->radius=$radius;  

7.         }  

8.   

9.         function area(){  

10.             return M_PI * $this->radius * $this->radius;  

11.         }  

12.   

13.         function perimeter(){  

14.             return 2 *  M_PI * $this->radius;  

15.         }  

16. }  

7、Result类

[php] view plain copy

1. <?php  

2.     class Result{  

3.         private $shape=null;//形状  

4.         private $result='';//结果  

5.   

6.         //通过不同的名字,创建不同的对象  

7.         function __construct(){  

8.             switch ($_POST["subForm"]){  

9.                 case "rectangle":  

10.                     $this->shape=new Rectangle($_POST["width"],$_POST["height"]);  

11.                     break;  

12.                 case "triangle":  

13.                     $this->shape=new Triangle($_POST["side1"],$_POST["side2"],$_POST["side3"]);  

14.                     break;  

15.                 case "circle":  

16.                     $this->shape=new Circle($_POST["radius"]);  

17.                     break;  

18.                 default:  

19.                     $this->shape=new Rectangle($_POST["width"],$_POST["height"]);  

20.             }  

21.         }  

22.         function __toString(){  

23.             $this->result.='面积是:'.$this->shape->area();  

24.             $this->result.='周长是:'.$this->shape->perimeter();  

25.             return $this->result;  

26.         }  

27.     }  

8、Form类

[php] view plain copy

1. <?php  

2.     class Form{  

3.         private $index;//位置  

4.   

5.         function __construct($index="index.php"){  

6.             $this->index=$index;  

7.         }  

8.   

9.         function __toString(){  

10.             $form='<form action="'.$this->index  .'?action='. $_GET["action"]  .'" method="post">';  

11.             switch ($_GET["action"]){  

12.                 case "rectangle":  

13.                     $form.=$this->getRectangle();  

14.                     break;  

15.                 case "triangle":  

16.                     $form.=$this->getTriangle();  

17.                     break;  

18.                 case "circle":  

19.                     $form.=$this->getCircle();  

20.                     break;  

21.                 default:  

22.                     $form.=$this->getRectangle();  

23.             }  

24.             $form.='<input type="text" hidden="hidden" name="subForm" value="'.$_GET["action"].'">';  

25.             $form.='<input type="submit" value="计算">';  

26.             $form.='</form>';  

27.             return $form;  

28.         }  

29.   

30.         function getRectangle(){  

31.             $rectangle='矩形:<br>';  

32.             $rectangle.='宽度:<input type="text" name="width">'.'<br>';  

33.             $rectangle.='高度:<input type="text" name="height">'.'<br>';  

34.             return $rectangle;  

35.         }  

36.         function getTriangle(){  

37.             $rectangle='三角形:<br>';  

38.             $rectangle.='边一:<input type="text" name="side1">'.'<br>';  

39.             $rectangle.='边二:<input type="text" name="side2">'.'<br>';  

40.             $rectangle.='边三:<input type="text" name="side3">'.'<br>';  

41.             return $rectangle;  

42.         }  

43.         function getCircle(){  

44.             $rectangle='圆形:<br>';  

45.             $rectangle.='半径:<input type="text" name="radius">'.'<br>';  

46.             return $rectangle;  

47.         }  

48.     }  

9、index.php

[php] view plain copy

1. <html lang="cn">  

2. <head>  

3.     <meta charset="UTF-8">  

4.     <title>图形计算</title>  

5. </head>  

6. <body>  

7.     <center>  

8.         <h1>计算图形</h1>  

9.         <a href="index.php?action=rectangle">矩形</a>  

10.         <a href="index.php?action=triangle">三角形</a>  

11.         <a href="index.php?action=circle">圆形</a>  

12.     </center>  

13.   

14.     <?php  

15. //        error_reporting(E_ERROR);  

16. //        ini_set("display_errors","Off");  //屏蔽掉PHP警告和错误提示  

17.   

18.         //类的自动加载  

19.         function __autoload($className){  

20.             include strtolower($className).".class.php";  

21.         }  

22.   

23.         //显示表单  

24.         echo new Form();  

25.         echo '<br><br><br><br>结果:<br>';  

26.         //操作数据  

27.         echo new Result();  

28.     ?>  

29. </body>  

30. </html>  

希望这篇文章可以帮助到你,总之同学们,IT知识尽在职坐标。

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved