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 < ;/font> '; Exit }}//Helen Formula 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 () {$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