PHP: Brother's Object-oriented graphics Calculator 2

Source: Internet
Author: User
Tags php class

In the previous section, the diversion by result.class.php, since three classes inherit the shape class, let's take a look at inheritance in object-oriented.

Shape.class.shape file

<?php     abstract class shape{public            $shapeName;            Abstract function area ();            Abstract function perimeter ();           /* * All entity classes inherit shape to facilitate uniform methods and properties          * Verify that the Validate method is consistent          * *             /protected function Validate ($value, $ Message= "Shape") {                      if ($value = = "" | |! Is_numeric ($value) | | $value < 0) {                           echo ' <font color= ' red ' > '. $message. ' must be a non-negative number > Word, and cannot be null </font><br> ';                           return false;       }              else{                           return True;}}     ? >

This is an abstract class, and the advantage is that the first one defines the unified properties and methods. This enables unified invocation of the $shapename attribute and area () and perimeter () in result.

Colleagues can also construct common methods, such as validating methods validate.

The following is a detailed implementation of each class

rect.class.php Implement RECT Rectangle class

<?phpclass Rect extends Shape {private $width = 0;private $height = 0;function __construct () {$this->shapename = "Rectangle "If" ($this->validate ($_post ["width"], ' rectangle widths ') & $this->validate ($_post ["height"], ' Rectangle Heights ') {$this-> width = $_post ["width"]; $this->height = $_post ["height"];} else {exit ();} $this->width = $_post ["width"]; $this->height = $_post ["height"];} function area () {return $this->width * $this->height;} function perimeter () {return 2 * ($this->width + $this->height);}}? >
triangle.class.php Implement triangle Triangle class

<?php class Triangle extends shape{private $side 1=0;             Private $side 2=0;             Private $side 3=0;                  function __construct () {$this->shapename= "triangle";                  if ($this->validate ($_post[' side1 '), ' the first side of the triangle ') && $this->validate ($_post[' side2 '], ' first side of the triangle ') && $this->validate ($_post[' side3 '), ' first side of triangle ') {$this->side1=$_post["Side1"                  ];                  $this->side2=$_post["Side2"];             $this->side3=$_post["Side3"]; } if (! $this->validatesum ()) {echo ' <font color= ' red ' > The sum of the triangles must be greater than the third side &lt                          ;/font> ';                  Exit }}//Helen Formula function area () {$s = ($this->side1+ $this->side2+ $this                  -&GT;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 () {$condition 1= ($this->side1+ $this->side                     2) > $this->side3;                     $condition 2= ($this->side1+ $this->side3) > $this->side2;                    $condition 3= ($this->side2+ $this->side3) > $this->side1;                     if ($condition 1 && $condition 2 && $condition 3) {return true;             } Else{return false;} }}?>
circle.class.php Realization Circle Circle Class

<?php     class Circle extends shape{             private $radius =0;             function __construct () {                    $this->shapename= "Circle";                    if ($this->validate ($_post[' radius '), ' radius of circle ') {                        $this->radius.$_post["radius"];                    } Else{exit;}                    $this->radius=$_post["radius"];             }             function area () {                     return pi () * $this->radius* $this->radius;             }             function perimeter () {                     return 2*pi () * $this->radius;             }     }? >
All classes inherit the shape class, implement the methods inside and assign values to the attributes, implement each class with different polymorphism using the construction method and the detailed calculation method, the same time for the external callers, the area () and the Perimter () method, which is encapsulated, are encapsulated into a class inside the method can be.

PHP: Brother's Object-oriented graphics Calculator 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.