刚刚学完php面向对象的编程,参考着高洛峰老师的php教程学习了这个实例。

效果图片:

以下是实现代码:index.php

图形计算(使用面向对象开发技术)

图形(周长&面积) 计算器

矩形

三角形

圆形


error_reporting(E_ALL & ~E_NOTICE);//提示错误的等级

// __autoload是php中的魔术方法,在用到类的时候自动调用

function __autoload($className){

//自动导入这个类

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

}

//输出表单,form类中有魔术方法__toString,因此可以直接输出类的对象引用,就是输出对象返回的字符串

echo new Form();

if(isset($_POST["sub"])){

//输出结果

echo new Result();//直接输出对象的引用表示

}

?>form.class.php_这是表单类_<?php

//根据index中提交的不同action的值返回不同的表单

class Form{

private $action; private $shape; //构造方法

function __construct($action=""){

$this->action = $action; $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect";

} function __toString()

{

// TODO: Implement __toString() method.

$form='

'; switch($this->shape){ case "rect": $form.=$this->getRect(); break; case "triangle": $form.=$this->getTriangle(); break; case "circle": $form.=$this->getCircle(); break; default: $form.='请选择一个形状
';

} $form.=''; $form.='

'; return $form;

} private function getRect(){

$input='请输入 | 矩形 | 的宽度和高度:

'; $input.='宽度:
'; $input.='高度:
'; $input.=''; return $input;

} private function getTriangle(){

$input='请输入 | 三角形 | 的三条边:

'; $input.='第一边:
'; $input.='第二边:
'; $input.='第三边:
'; $input.=''; return $input;

} private function getCircle(){

$input='请输入 | 圆形 | 的半径:

'; $input.='半径:
'; $input.=''; return $input;

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 16:26

*

*/shape.class.php 这是一个抽象类,用来定义规范的<?phpabstract class Shape {

public $shapeName; //规范circle、triangle、rect中必须有area()、perimeter()方法

abstract function area();

abstract function perimeter();

public function setShapeName($shapeName)

{

$this->shapeName = $shapeName; return $this;

} //判断输入的数字是否为大于0的有效数字

protected function validate($value, $message="形状"){

if($value == "" || !is_numeric($value) || $value < 0 ){ echo ' '.$message.' 必须为非负值的数字,并且不能为空
'; return false;

} else { return true;

}

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 16:42

*/circle.class.php_就是计算周长和面积的公式了_<?phpclass Circle extends Shape {

private $radius=0; function __construct(){

$this->shapeName="圆形"; if($this->validate($_POST['radius'], '圆的半径')){ $this->radius=$_POST["radius"];

}else{ exit;

}

} function area(){

return pi()*$this->radius*$this->radius;

} function perimeter(){

return 2*pi()*$this->radius;

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 17:06

*/rect.class.php<?phpclass 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"];

} else{ exit;

}

} function area(){

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

} function perimeter()

{

return 2 * ($this->width + $this->height);

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 17:02

*/triangle.class.php<?phpclass Triangle extends Shape{

private $side1=0; private $side2=0; private $side3=0; function __construct(){

$this->shapeName="三角形"; if($this->validate($_POST['side1'], '三角形的第一个边')){ $this->side1=$_POST["side1"];

} if($this->validate($_POST['side2'], '三角形的第二个边')){ $this->side2=$_POST["side2"];

} if($this->validate($_POST['side3'], '三角形的第三个边')){ $this->side3=$_POST["side3"];

} if(!$this->validateSum()){ echo '三角形的两边之和必须大于第三边'; exit;

}

} function area(){

$s=( $this->side1+$this->side2+$this->side3 )/2; return sqrt( $s * ($s - $this->side1) * ($s - $this->side2) * ($s - $this->side3) );

} function perimeter(){

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

} private function validateSum()

{

$condition1 = ($this->side1 + $this->side2) > $this->side3; $condition2 = ($this->side1 + $this->side3) > $this->side2; $condition3 = ($this->side2 + $this->side3) > $this->side1; if ($condition1 && $condition2 && $condition3) { return true;

} else { return false;

}

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 17:04

*/result.class.php_这里是返回计算结果类_<?php

class Result{ private $shape;

function __construct()

{ switch($_POST['action']){ case 'rect':

$this->shape=new Rect(); break; case 'triangle':

$this->shape=new Triangle(); break; case 'circle':

$this->shape=new Circle(); break; default:

$this->shape=false;

}

} /**

* @return string

*/

function __toString()

{ // TODO: Implement __toString() method.

if($this->shape){

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

$result.=$this->shape->shapeName.'的面积:'.$this->shape->area().'
'; return $result;

}else{ return '没有这个形状';

}

}

}/**

* Created by PhpStorm.

* User: user

* Date: 2018/4/15

* Time: 16:47

*/

相关推荐:

php圆的周长_php面向对象编程练习:计算矩形、三角形、圆形的周长和面积相关推荐

  1. Java6面向对象编程创建一个矩形类, 求周长 面积

    package job; /** */ import java.util.Scanner; public class Space { /* 面向对象编程创建一个矩形类, 求周长 面积 */ publi ...

  2. 计算矩形和圆形的面积和周长(java)

    package exe02; import java.util.Scanner; /*name:Excise1 功能:计算矩形和圆形的面积和周长*/ public class second1 {pub ...

  3. Java求矩形三角形圆形梯形的面积和周长小程序页面

    用java实现图形面积周长计算器 1.一个接口 public interface Shape {default void GetArea()//面积{}default void GetPerimett ...

  4. java中矩形和圆形的周长面积_抽象类实现圆和矩形周长和面积的计算

    Shape.class package com.imooc; public abstract class Shape { abstract void circumference(); //周长 abs ...

  5. 计算正方形长方形圆形的周长面积

    初用Visual studio 2022,当你使用传统的"scanf"的时候会出现下面错误: 错误里面有这样一句话:use _CRT_SECURE_NO_WARNINGS 这个时候 ...

  6. 面向对象编程概念_如何向6岁的孩子解释面向对象的编程概念

    面向对象编程概念 by Alexander Petkov 通过亚历山大·佩特科夫(Alexander Petkov) Have you noticed how the same cliche ques ...

  7. python面向对象编程

    小白如何成为python数据分析师 第 十五 天---->面向对象编程 你有对象吗?下次遇到有人这样问,你就可以直接回答他,谁都有对象,因为一切皆为对象. 我们之前的编程都是指令式编程,即输入指 ...

  8. day17-csv文件excel文件操作和面向对象编程

    day17 - csv文件和excel文件操作和面向对象编程(10.12) 1.python读取csv文件 python读取csv文件–读取csv文件 建立文件夹-ctrl+c–ctrl+v加入csv ...

  9. 【Java开发语言 03】第三章 面向对象编程(面向对象与面向过程+类和对象+类成员一:属性+类成员二:方法+对象的创建和使用+封装和隐藏+构造器+关键字this,package,import)

    面向对象编程 1 面向对象与面向过程 1.1 java类及类的成员 2 java语言的基本元素:类和对象 2.1 类的语法格式 2.2 创建Java自定义类 2.3 对象的创建及使用 3 类的成员之一 ...

最新文章

  1. C语言 #ifndef 引起的redefinition of xxx 问题解决
  2. 本科生如何自学机器学习?
  3. NLTK包和语料库的准备
  4. java类和接口实例_Java定义泛型接口和类的方法实例分析
  5. 【loj6342】跳一跳 期望dp
  6. 阅读日志:协鑫一年的多晶硅料产能能够生产做少装机容量的组件?
  7. matlab叶子分割实验,基于MATLAB进行树叶面积测量实验报告
  8. 原生javascript
  9. 小括号教学设计导入_【教资面试】语文政治历史地理教学设计答题技巧!
  10. 概率论在计算机科学的应用讲座,概率论导论(翻译版)
  11. 鸟哥的linux私房菜-服务器架设篇 读书笔记
  12. Redis入门官方文档
  13. 【第一组】第十三次例会纪要
  14. hdu 2036 改革春风吹满地(叉积求多边形面积)
  15. 润乾报表Api导出word只读
  16. multisim红绿灯元器件在哪里_基于Multisim的红绿灯控制器仿真实现
  17. 《Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks》论文翻译
  18. 数据库插入数据报错Unknown error finalizing or resetting statement (19: UNIQUE constraint failed:xxx.xxx)
  19. 基于pytest框架自动化测试脚本的编写 -入门到精通
  20. 自动洗车车牌识别系统

热门文章

  1. 第一次作业:我和计算机
  2. ubuntu企业微信和微信无法发送和显示图片和截图问题
  3. html403禁止访问怎么解决,http出现“禁止访问 403”错误的起因和解决方法
  4. 汽车网络安全:TEE是否能够取代HSM?
  5. 血氧仪电路方案开发--PCBA方案
  6. 数组(一维数组与二维数组)
  7. 服务器开启防火墙后无法登录系统,客户端连不上,服务器防火墙服务启动不了...
  8. awk详解:awk语言及应用
  9. 单片机低功耗配置及注意事项
  10. [Validation set: 'activiti-executable-process' | Problem: 'activiti-exclusive-gateway-condition-not-