search
Homephp教程php手册The sixth part of the object-oriented series of front-end learning PHP: Implementation of a simple graphic area calculator

Previous words

This article uses object-oriented technology to implement a simple graphic area calculator

Graphics

<span style="color: #008000;">//</span><span style="color: #008000;">rect.class.php</span>
<span style="color: #000000;">php
    </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Shape{
        </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$name</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> area();
        </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> view();
        </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span> test(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
    }
</span>?>

Main interface

<span style="color: #008000;">//</span><span style="color: #008000;">index.php</span>



<meta charset="UTF-8">
<title>Document</title>
<style>
.<span style="color: #000000;">box{
    width:<span style="color: #000000;"> 800px;
    margin: 0<span style="color: #000000;"> auto;
}
</style>


<div style="color: #0000ff;">class="box">
    <h1 id="图形计算器">图形计算器</h1>
    <div>
        <a href="index.php?action=rect">矩形</a>
        <a href="index.php?action=triangle">三角形</a>
    </div>
</div>    
    <span style="color: #000000;">php
        </span><span style="color: #008080;">error_reporting</span>(<span style="color: #ff00ff;">E_ALL</span> & ~<span style="color: #ff00ff;">E_NOTICE</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$classname</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">include</span> <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$classname</span>).".class.php"<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">])) {
            </span><span style="color: #800080;">$classname</span> = <span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">]);
            </span><span style="color: #800080;">$shape</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$classname</span>(<span style="color: #800080;">$_POST</span><span style="color: #000000;">);
            </span><span style="color: #800080;">$shape</span>-><span style="color: #000000;">view();
            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['dosubmit'<span style="color: #000000;">])) {
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$shape</span>->test(<span style="color: #800080;">$_POST</span><span style="color: #000000;">)) {
                    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$shape</span>->name."的面积为:".<span style="color: #800080;">$shape</span>->area()."<br>"<span style="color: #000000;">;
                }
            }
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #0000ff;">echo</span> "请选择一个要计算的图形!<br>"<span style="color: #000000;">;
        }
    </span>?>

Rectangular class

<span style="color: #008000;">//</span><span style="color: #008000;">rect.class.php</span>
<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span> Rect <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Shape{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$width</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$height</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$arr</span>=<span style="color: #000000;">[]){
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$this</span>->width = <span style="color: #800080;">$arr</span>['width'<span style="color: #000000;">];
            </span><span style="color: #800080;">$this</span>->height = <span style="color: #800080;">$arr</span>['height'<span style="color: #000000;">];
        }
        </span><span style="color: #800080;">$this</span>->name = "矩形"<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> area() {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->width * <span style="color: #800080;">$this</span>-><span style="color: #000000;">height;
    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> view() {
        </span><span style="color: #800080;">$form</span> = '
'; $form .=$this->name.'的宽:
'; $form .=$this->name.'的高:
'; $form .='
'; $form .='
'; echo $form; } function test($arr) { $bg = true; if($arr['width'] ) { echo $this->name."的宽不能小于0!
"; $bg = false; } if($arr['height'] ) { echo $this->name."的高度不能小于0!
"; $bg = false; } return $bg; } } ?>

Triangle class

<span style="color: #008000;">//</span><span style="color: #008000;">triangle.class.php</span>
<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span> Triangle <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Shape{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$b1</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$b2</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$b3</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$arr</span>=<span style="color: #000000;">[]){
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$this</span>->b1 = <span style="color: #800080;">$arr</span>['b1'<span style="color: #000000;">];
            </span><span style="color: #800080;">$this</span>->b2 = <span style="color: #800080;">$arr</span>['b2'<span style="color: #000000;">];
            </span><span style="color: #800080;">$this</span>->b3 = <span style="color: #800080;">$arr</span>['b3'<span style="color: #000000;">];
        }
        </span><span style="color: #800080;">$this</span>->name = "三角形"<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> area() {
        </span><span style="color: #800080;">$p</span> = (<span style="color: #800080;">$this</span>->b1 + <span style="color: #800080;">$this</span>->b2 + <span style="color: #800080;">$this</span>->b3)/2<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">sqrt</span>(<span style="color: #800080;">$p</span>*(<span style="color: #800080;">$p</span>-<span style="color: #800080;">$this</span>->b1)*(<span style="color: #800080;">$p</span>-<span style="color: #800080;">$this</span>->b2)*(<span style="color: #800080;">$p</span>-<span style="color: #800080;">$this</span>-><span style="color: #000000;">b3));
    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> view() {
        </span><span style="color: #800080;">$form</span> = '
'; $form .=$this->name.'第一个边的宽:
'; $form .=$this->name.'第二个边的宽:
'; $form .=$this->name.'第三个边的宽:
'; $form .='
'; $form .='
'; echo $form; } function test($arr) { $bg = true; if($arr['b1'] ) { echo "第一个边的宽不能小于0!
"; $bg = false; } if($arr['b2'] ) { echo "第二个边的宽不能小于0!
"; $bg = false; } if($arr['b3'] ) { echo "第三个边的宽不能小于0!
"; $bg = false; } if(($arr['b1'] + $arr['b2'] $arr['b3'])||($arr['b1'] + $arr['b3'] $arr['b2'])||($arr['b3'] + $arr['b2'] $arr['b1'])){ echo '两边之和不能小于第三边
'; $bg = false; } return $bg; } } ?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.