index.php  用于显示页面

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>图形计算器</title>
</head>
<body>
<center>
<h1>图形(周长&面积)计算器</h1>
<a href = "index.php?action=rect">矩形</a>
<a href = "index.php?action=circle">圆形</a>
</center>
<?php
//错误级别设置为除了注意全部输出,避免输出变量未定义等消息
error_reporting(E_ALL & ~E_NOTICE); //使用自动加载类技术
function __autoload($className){
include strtolower($className).".class.php";
} //实际是调用了Form类的__toString方法;
echo new Form("index.php"); if(isset($_POST["sub"])) {
echo new Result();
}
?>
</body> </html>

form.class.php  根据action不同(rect,circle)显示不同的form

 <?php
class Form {
private $action;
private $shape; function __construct($action = "") {
$this->action = $action;
//默认rect;
$this->shape = isset($_GET['action']) ? $_GET['action'] : "rect";
} function __toString() {
$form = '<form action = "'.$this->action.'?action='.$this->shape.'" method="post">'; //根据get请求组成方法名称字符串,例如GetRect()
$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 getCircle() {
$input = '<b>请输入 | 圆形 | 的半径: </b><p>';
$input .= '半径: <input type="text" name="radius" value="'.$_POST["radius"].'"/><br/>';
return $input;
} }
?>

result.class.php  根据action构造对应的类并输出结果

 <?php
class Result {
private $shape = null;
//构造方法,根据action类新建类
function __construct(){
$this->shape = new $_GET['action']();
}
//利用多态性根据形状自动计算周长和面积
function __toString(){
$result = $this->shape->shapeName.'的周长: '.round($this->shape->perimeter(), 2).'<br/>';
$result .= $this->shape->shapeName.'的面积: '.round($this->shape->area(), 2).'<br/>';
return $result;
}
} ?>

shape.class.php  图形的基类,定义了计算面积和周长的抽象方法

 <?php
abstract class Shape {
public $shapeName;
//定义两个抽象方法,计算面积和周长
abstract function area();
abstract function perimeter(); //验证数据合法性的方法
protected function validate($value, $messgae = "输入值") {
if ($value==""||!is_numeric($value)||$value < 0) {
$messgae = $this->shapeName.$messgae;
echo '<font colr="red">'.$messgae.'不合法</font><br/>';
return false;
}
return true;
}
} ?>

rect.class.php  矩形的实现

 <?php
class 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"];
}
} public function area() {
return $this->width * $this->height;
}
public function perimeter() {
return 2 * ($this->width + $this->height);
}
}
?>

circle.class.php  圆形的实现

 <?php
class Circle extends Shape {
private $radius = 0;
function __construct(){
$this->shapeName = "圆形";
if ($this->validate($_POST["radius"], "半径")) {
$this->radius = $_POST["radius"];
}
} public function area() {
return pi() * $this->radius * $this->radius;
}
public function perimeter() {
return 2 * pi() * $this->radius;
}
}
?>

页面结果

PHP学习笔记06——面向对象版图形计算器的更多相关文章

  1. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

  2. php:兄弟连之面向对象版图形计算器2

    上篇说到通过result.class.php来分流,因为三个类都继承了shape这个类,让我们来看一下,面向对象中的继承. shape.class.shape文件 <?php abstract ...

  3. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  4. UML和模式应用学习笔记-1(面向对象分析和设计)

    UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...

  5. 机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记

    机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记 关键字:k-均值.kMeans.聚类.非监督学习作者:米仓山下时间: ...

  6. iOS学习笔记06—Category和Extension

    iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...

  7. Lua学习笔记:面向对象

    Lua学习笔记:面向对象 https://blog.csdn.net/liutianshx2012/article/details/41921077 Lua 中只存在表(Table)这么唯一一种数据结 ...

  8. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  9. Java学习笔记之---面向对象

    Java学习笔记之---面向对象 (一)封装 (1)封装的优点 良好的封装能够减少耦合. 类内部的结构可以自由修改. 可以对成员变量进行更精确的控制. 隐藏信息,实现细节. (2)实现封装的步骤 1. ...

随机推荐

  1. ExtJs之Ext.util.MixedCollection

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  2. 妙味课堂——HTML+CSS(第四课)(一)

    这一课学的东西真是太多了,还不赶快记下来,留待以后慢慢回味! 首先我们回顾一下inline-block的特性: 使块元素在一行显示 使内嵌支持宽高 换行被解析了(问题) 不设置宽度的时候,宽度由内容撑 ...

  3. java获取系统当前时间

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式 System.out.print(df. ...

  4. 2016CVTE编程题:兔子藏洞

    兔子藏洞 题目描述 一只兔子藏身于20个圆形排列的洞中(洞从1开始编号),一只狼从x号洞开始找,下次隔一个洞找(及在x+2号洞找),在下次个两个洞找(及在x+5号洞找),以此类推...它找了n次仍然没 ...

  5. SVN的使用(转载)

    MyEclipse中的SVN操作手册   导入项目 点击工具栏中的File-Import,进入下图: 点击Nex进入下图: 点击Next进入下图,输入你SVN服务器的IP地址,包括端口号和文件夹等完整 ...

  6. 面向 Java 开发人员的 Ajax: 构建动态的 Java 应用程序

    面向 Java 开发人员的 Ajax: 构建动态的 Java 应用程序 Ajax 为更好的 Web 应用程序铺平了道路 在 Web 应用程序开发中,页面重载循环是最大的一个使用障碍,对于 Java™ ...

  7. mongo中查询Array类型的字段中元素个数

    I have a MongoDB collection with documents in the following format: { "_id" : ObjectId(&qu ...

  8. Java:IO流与IO设备

    打印流:PrintWriter和PrintStream 特点:可以直接操作输入流和文件 //例子1:使用PrintStream将格式化的日期打印到文件中 import java.io.*; impor ...

  9. SSIS ->> Control Flow And Data Flow

    In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...

  10. C# 将字符串转化成流,将流转换成字符串

    using System; using System.IO; using System.Text; namespace CSharpConvertString2Stream { class Progr ...