Heim > php教程 > php手册 > Der sechste Teil der objektorientierten Reihe zum Front-End-Lernen von PHP: Implementierung eines einfachen grafischen Flächenrechners

Der sechste Teil der objektorientierten Reihe zum Front-End-Lernen von PHP: Implementierung eines einfachen grafischen Flächenrechners

WBOY
Freigeben: 2016-11-16 10:24:01
Original
1523 Leute haben es durchsucht

Vorherige Wörter

Dieser Artikel verwendet objektorientierte Technologie, um einen einfachen Grafikflächenrechner zu implementieren

Grafiken

<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>?>
Nach dem Login kopieren

Hauptschnittstelle

<span style="color: #008000;">//</span><span style="color: #008000;">index.php</span>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.<span style="color: #000000;">box{
    width</span>:<span style="color: #000000;"> 800px;
    margin</span>: 0<span style="color: #000000;"> auto;
}
</span></style>
</head>
<body>
<div <span style="color: #0000ff;">class</span>="box">
    <h1>图形计算器</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>?>
</body>
</html>
Nach dem Login kopieren

Rechteckige Klasse

<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 action="index.php?action=rect" method="post">'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .=<span style="color: #800080;">$this</span>->name.'的宽:<input name="width" value=""/><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .=<span style="color: #800080;">$this</span>->name.'的高:<input name="height" value=""/><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .='<input type="submit" name="dosubmit" value="计算"><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .='</form>'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$form</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">function</span> test(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {
        </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>['width'] < 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>->name."的宽不能小于0!<br>"<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;    
        }
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>['height'] < 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>->name."的高度不能小于0!<br>"<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$bg</span><span style="color: #000000;">;
    }
}
</span>?>
Nach dem Login kopieren

Dreiecksklasse

<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 action="index.php?action=triangle" method="post">'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .=<span style="color: #800080;">$this</span>->name.'第一个边的宽:<input name="b1" value=""/><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .=<span style="color: #800080;">$this</span>->name.'第二个边的宽:<input name="b2" value=""/><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .=<span style="color: #800080;">$this</span>->name.'第三个边的宽:<input name="b3" value=""/><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .='<input type="submit" name="dosubmit" value="计算"><br>'<span style="color: #000000;">;
        </span><span style="color: #800080;">$form</span> .='</form>'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$form</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">function</span> test(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {
        </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>['b1'] < 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> "第一个边的宽不能小于0!<br>"<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;    
        }
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>['b2'] < 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> "第二个边的宽不能小于0!<br>"<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;    
        }
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>['b3'] < 0<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> "第三个边的宽不能小于0!<br>"<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;    
        }
        </span><span style="color: #0000ff;">if</span>((<span style="color: #800080;">$arr</span>['b1'] + <span style="color: #800080;">$arr</span>['b2'] < <span style="color: #800080;">$arr</span>['b3'])||(<span style="color: #800080;">$arr</span>['b1'] + <span style="color: #800080;">$arr</span>['b3'] < <span style="color: #800080;">$arr</span>['b2'])||(<span style="color: #800080;">$arr</span>['b3'] + <span style="color: #800080;">$arr</span>['b2'] < <span style="color: #800080;">$arr</span>['b1'<span style="color: #000000;">])){
            </span><span style="color: #0000ff;">echo</span> '两边之和不能小于第三边<br>'<span style="color: #000000;">;
            </span><span style="color: #800080;">$bg</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$bg</span><span style="color: #000000;">;
    }
}
</span>?>
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage