ddxiami

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3220|回复: 6

[细说PHP] 第10章PHP面向对象的程序设计--面向对象版图形计算器

[复制链接]
发表于 2013-8-7 13:33:14 | 显示全部楼层 |阅读模式
文件:index.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
                        //设置错误报告的级别,除了无关紧要的"注意",其它的所有报告都输出
                        error_reporting(E_ALL & ~E_NOTICE);

                        //通过魔术方法__autoload()去自动加载所需要的类文件,将需要的类包含进来
                        function __autoload($className){
                                include strtolower($className).".class.php";   //包含类所在的文件               
                        }

                        echo new Form("index.php");           //输出用户需要的表单
       
                        //如果用户提交表单则去计算
                        if(isset($_POST["sub"])) {
                                echo new Result();         //输出形状的计算结果       
                        }
                ?>               
        </body>
</html>
回复

使用道具 举报

 楼主| 发表于 2013-8-7 13:33:40 | 显示全部楼层
文件:form.class.php

<?php       
        /**
                Project: 面向对象版图形计算器
                file: form.class.php
                选择不同的图形时输出对应的表单对象,在主脚本程序中提供给用户一个操作界面
                package:shape
         */
        class Form {
                private $action;                                                    //表单form属性action的值, 用于设置表单提交的位置
                private $shape;                                             //用户选择形状的动作

                /* 构造方法,用于对用户的操作动作和表单提交的位置进行初使化赋值 */
                function __construct($action=""){
                        $this->action = $action;                      //为表单form中的action属性赋值
                        /* 用户选择形状的动作,默认为矩形rect */
                        $this->shape = isset( $_GET["action"] ) ? $_GET["action"] : "rect";  
                }

                /* 魔术方法__toString(), 通过用户不同的请求,返回用户需要的表单字符串,在页面中显示 */
                function __toString(){
                        $form='<form action="'.$this->action.'?action='.$this->shape.'" method="post">';
                       
                        /* 根据用户的get请求组合成方法名称字符串,getRect(), getTriangle(), getCircle() */
                        $shape = "get".ucfirst($this->shape);
                        $form .= $this->$shape();                    //调用私有方法获取圆形的输入表单
                                       
                        $form .= '<br><input type="submit" name="sub" value="计算"><br>';
                        $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>';
                        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>';
                        return $input;
                }

                /* 私有方法,用于获取圆形的表单输入 */
                private function getCircle(){
                        $input = '<b>请输入 | 圆形 | 的半径:</b><p>';
                        $input .= '半径:<input type="text" name="radius" value="'.$_POST["radius"].'"><br>';
                        return $input;
                }
        }




回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-7 13:34:07 | 显示全部楼层
文件:shape.class.php

<?php       
        /**
                Project: 面向对象版图形计算器
                file:shape.class.php
                声明一个形状的抽象类,作为所有形状的父类,里面有两个抽象方法,根据子类的形状去实现
                package:shape
        */
        abstract class Shape {                    
                public $shapeName;                                  //形状的名称
                abstract function area();         //声明的抽象方法在子类中实现它,用来计算不同图型的面积
                abstract function  perimeter();   //声明的抽象方法在子类中实现它,用来计算不同图型的周长

                /*
                 * 该方法是一个普通方法用来对所有形状表单输入的值进行验证
                 * @param        mixed        $value        接收表单输入的值,对该值进行验证
                 * @param        string        $message        消息提示的前缀
                 * @return        boolean                        验正通过返回true, 失败则返回false
                 */
                protected function validate($value, $message = '输入的值' ){
                        if( $value=="" || !is_numeric($value) || $value < 0 ) {
                                $message=$this->shapeName.$message;
                                echo '<font color="red">'.$message.'必须为非负值的数字,并且不能为空</font><br>';
                                return false;
                        }else{
                                return true;
                        }
                }
        }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-7 13:34:40 | 显示全部楼层
文件: result.class.php

<?php       
        /**
                Project: 面向对象版图形计算器
                file: result.class.php
                声明一个Result结果类,通过多态的应用获取用户所选择形状的计算结果
                package:shape
        */
        class Result {
                private $shape = null ;             //成员属性用于获取某一形状对象

                /* 构造方法用于初使化成员属性$shape */
                function __construct(){
                        /* 根据用户的get方法提交的动作'action'创建对应的形状对象[$_GET['action']()变量函数技术] */
                        $this->shape = new $_GET['action']();
                }

                /* 声明一个魔术方法__toString,在直接访问该对象引用时自动调用,返回利用多态计算后的结果字符串 */
                function __toString(){
                        //调用形状对象中的周长方法,获得周长的值
                        $result = $this->shape->shapeName.'的周长:'.round($this->shape->perimeter(), 2).'<br>';
                        //调用形状对象中的面积方法,获得面积的值
                        $result .= $this->shape->shapeName.'的面积:'.round($this->shape->area(), 2).'<br>';
               
                        return $result;                                        //返回计算结果字符串
                }       
        }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-7 13:35:00 | 显示全部楼层
文件:rect.class.php

<?php       
        /**
                Project: 面向对象版图形计算器
                file:rect.class.php
                声明了一个矩形子类,根据矩形的特点实现了形状抽象类中的周长和面积方法
                package:shape
        */
        class Rect extends Shape {         
                private $width = 0;                   //声明矩形的成员属性宽度
                private $height = 0;                   //声明矩形的成员属性高度

                /* 矩形的构造方法,用表单$_POST中接收的高度和宽度初使化矩形对象 */
                function __construct() {   
                        $this->shapeName = "矩形";    //为形状命名

                        //通过从shape中继承的方法validate(),对矩形的宽度和高度进行验证
                        if($this->validate($_POST["width"], "宽度") & $this->validate($_POST["height"], "高度")){
                                $this->width = $_POST["width"];   //通过超全局数组$_POST将表单输入的宽度给成员属性width赋初值
                                $this->height = $_POST["height"]; //通过超全局数组$_POST将表单输入高度给成员属性height赋初值
                        }
                }

                /* 按矩形面积的计算公式,实现抽象类shape中的抽象方法area() */
                function area() {                     
                        return $this->width*$this->height;       //访问该方法时,返回矩形的面积
                }

                /* 按矩形周长的计算公式,实现抽象类shape中的抽象方法perimeter() */
                function perimeter() {         
                        return 2*($this->width+$this->height);   //访问该方法时,返回矩形的周长
                }
        }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-7 13:35:23 | 显示全部楼层
文件:triangle.class.php

<?php       
        /**
                Project: 面向对象版图形计算器 
                file: triangle.class.php
                声明了一个三角形子类,根据三角形的特点实现了形状抽象类中的周长和面积方法
                package:shape
         */
        class Triangle extends Shape {         
                private $side1 = 0;                           //声明三角形第一个边的成员属性
                private $side2 = 0;                           //声明三角形第二个边的成员属性
                private $side3 = 0;                           //声明三角形第三个边的成员属性




                /* 三角形的构造方法,用表单$_POST中接收的三边值初使化三角形对象 */
                function __construct() { 
                        $this->shapeName = "三角形";                  //为形状命名
                       
                        //通过从shape中继承的方法validate(),对三角形的第一边进行验证
                        if($this->validate($_POST["side1"], "第一个边") & 
                                        $this->validate($_POST["side2"], "第二个边") & 
                                                $this->validate($_POST["side3"], "第三个边")) {
                                //通过本类内部的私有方法validateSum(),验证三角形的两边之和是否大于第三边
                                if($this->validateSum($_POST["side1"], $_POST["side2"], $_POST["side3"])) {
                                        $this->side1 = $_POST["side1"];                
                                        $this->side2 = $_POST["side2"];                
                                        $this->side3 = $_POST["side3"];                
                                }else {
                                        echo '<font color="red" >三角形的两边之和要大于第三边</font><br>';
                                }
                        }
                }




                /* 按三角形面积的计算公式(海伦公式),实现抽象类shape中的抽象方法area() */
                function area() {
                        $s = ($this->side1+$this->side2+$this->side3)/2;
                        //返回三角形的面积
                        return sqrt( $s*($s - $this->side1)*($s - $this->side2)*($s - $this->side3) );  
                }




                /* 按三角形周长的计算公式,实现抽象类shape中的抽象方法perimeter() */
                function perimeter() {   
                        return  $this->side1+$this->side2+$this->side3;        //返回三角形的周长
                }




                /* 本类内部声明一个私有方法validateSum(),用于验证三角形的两边之和是否大于第三边 */
                private function validateSum($s1, $s2, $s3){
                        //如果三角形任意两个边的和大于第三个边返回true, 否则返回false
                        if( (($s1 + $s2) > $s3) && (($s1 + $s3) > $s2) && (($s2 + $s3) > $s1) ) {
                                return true;
                        }else{
                                return false;
                        }       
                }
        }




       
       
       




回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-7 13:35:53 | 显示全部楼层
文件:circle.class.php

<?php
        /**
                Project: 面向对象版图形计算器
                file: circle.class.php
                声明了一个圆形子类, 按圆形的特点实现了形状抽象类Shape中的周长和面积
                package:shape
        */
        class  Circle extends Shape {         
                private $radius = 0;                    //声明一个成员属性用于存储圆形的半径

                /* 圆形的构造方法,用表单$_POST中接收的半径初使化圆形对象*/
                function __construct() {     
                        $this->shapeName = "圆形";                    //为形状命名

                        //通过从shape中继承的方法validate(),对圆形的半径进行验证
                        if( $this->validate($_POST['radius'], '半径') ) {
                                $this->radius = $_POST['radius'];  
                        }
                }
               
                /* 按圆形面积的计算公式,实现抽象类shape中的抽象方法area() */
                function area() {           
                        return pi() * $this->radius * $this->radius;   //返回圆形的面积
                }

                /* 按圆形周长的计算公式,实现抽象类shape中的抽象方法perimeter() */
                function perimeter() {      
                        return 2 * pi() * $this->radius;              //返回圆形的周长
                }
        }       
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|技术文档库 ( 闽ICP备15017263号-2 )|网站地图

GMT+8, 2025-5-18 18:14 , Processed in 0.041535 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表