图形计算器(抽象类的简单小应用)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/my_study_everyday/article/details/88812445

最终效果图

图形计算器

首先定义shape抽象类

shape.class.php文件内容如下:

/**
* 这是一个抽象类
* 定义子类必须实现的一些方法
*/
abstract class Shape
{
	// 形状的名称
	public $name;

	// 形状的面积
	abstract function area();

	// 形状的周长
	abstract function girth();

	// 形状的图形界面
	abstract function view();

	// 形状的校验
	abstract function validate($arr);
}

接着定义rect类继承自抽象类shape

rect.class.php文件内容如下:

/**
* 这是一个矩形的类,这个类要按照形状的规范实现
*/
class Rect extends Shape
{
	private $width;
	private $height;
	public $name = "矩形";

	function __construct($arr=array()){
		if(!empty($arr)){
			$this->width = $arr['width'];
			$this->height = $arr['height'];
		}
	}

	function area(){
		return $this->width * $this->height;
	}
	function girth(){
		return 2*($this->width * $this->height);
	}
	function view(){
		$form = '<form action="index.php?action=rect" method="post">';
		$form .= $this->name.'的宽:<input type="text" name="width" value="'. $_POST["width"].'"><br>';
		$form .= $this->name.'的高:<input type="text" name="height" value="'. $_POST["height"].'"><br>';
		$form .= '<input type="submit" name="doSubmit" value="计算"><br>';
		$form .= '</form>';
		return $form;
	}
	function validate($arr){
		$flag = true;
		if ($arr["width"] < 0) {
			echo $this->name."的宽不能小于0<br>";
			$flag = false;
		}

		if ($arr["height"] < 0) {
			echo $this->name."的高不能小于0<br>";
			$flag = false;
		}

		return $flag; 
	}
}

再定义一个triangle类继承自抽象类shape

triangle.class.php文件内容如下:

/**
* 这是一个三角形的类,这个类要按照形状的规范实现
*/
class Triangle extends Shape
{
	private $side1;
	private $side2;
	private $side3;
	public $name = "三角形";

	function __construct($arr=array()){
		if(!empty($arr)){
			$this->side1 = $arr['side1'];
			$this->side2 = $arr['side2'];
			$this->side3 = $arr['side3'];
		}
	}

	function area(){
		$a = $this->side1;
		$b = $this->side2;
		$c = $this->side3;
		$p = ($a+$b+$c)/2;

		return sqrt($p*($p-$a)*($p-$b)*($p-$c));
	}
	function girth(){
		return $this->side1 + $this->side2 + $this->side3;
	}
	function view(){
		$form = '<form action="index.php?action=triangle" method="post">';
		$form .= $this->name.'的边1:<input type="text" name="side1" value="'. $_POST["side1"].'"><br>';
		$form .= $this->name.'的边2:<input type="text" name="side2" value="'. $_POST["side2"].'"><br>';
		$form .= $this->name.'的边3:<input type="text" name="side3" value="'. $_POST["side3"].'"><br>';
		$form .= '<input type="submit" name="doSubmit" value="计算"><br>';
		$form .= '</form>';
		return $form;
	}
	function validate($arr){
		$flag = true;
		if ($arr["side1"] < 0) {
			echo $this->name."的side1不能小于0<br>";
			$flag = false;
		}
		if ($arr["side2"] < 0) {
			echo $this->name."的side2不能小于0<br>";
			$flag = false;
		}
		if ($arr["side3"] < 0) {
			echo $this->name."的side3不能小于0<br>";
			$flag = false;
		}

		if ($arr["side1"] + $arr["side2"] <= $arr["side3"] || $arr["side1"] + $arr["side3"] <= $arr["side2"]||$arr["side2"] + $arr["side3"] <= $arr["side1"]) {
			echo $this->name."的两边之和必须大于第三边<br>";
			$flag = false;
		}

		return $flag; 
	}
}

实现业务逻辑

index.php的内容如下:

<?php
header("Content-type:text/html;charset=utf8");
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>图形计算器</title>
</head>
<body>
<center>
	<h1>图形计算器</h1>
	<a href="index.php?action=rect">矩形</a> || <a href="index.php?action=triangle">三角形</a>
	<hr>
</center>
</body>
</html>

<?php
// 设置错误报告级别
error_reporting(E_ALL & ~E_NOTICE);
// 设置自动加载这个程序需要的类
function __autoload($className){
	include strtolower($className).".class.php";
}


// 判断用户是否有单击一个形状链接
if (!empty($_GET["action"])) {
	//第一步: 创建形状的对象
	$classname = ucfirst($_GET["action"]);
	if (isset($_POST['doSubmit'])) {
		$shape = new $classname($_POST);
	}else{
		$shape = new $classname();
	}
	//第二步: 创建形状的表单界面
	echo $shape->view();

	//第三步: 用户是否提交了表单
	if (isset($_POST['doSubmit'])) {
		//第四步: 验证表单
		if($shape->validate($_POST)){
			//第五步: 输出图形的计算结果
			echo $shape->name ."的面积是:".$shape->area()."<br>";
			echo $shape->name ."的周长是:".$shape->girth()."<br>";
		}
	}
}else{
	echo "请选择一个要计算的图形";
}

 ?>

猜你喜欢

转载自blog.csdn.net/my_study_everyday/article/details/88812445