Use the object-oriented graphic calculator and the object-oriented graphic calculator _ PHP Tutorial

Source: Internet
Author: User
Use an object-oriented graphic calculator. Using an object-oriented graphic calculator, this example may not be practical, but basically summarizes three features of object-oriented: inheritance, encapsulation, and polymorphism. Use an object-oriented graphic calculator

This example may not be practical, but basically summarizes three features of object-oriented: inheritance, encapsulation, and polymorphism. The main functions of this example are as follows:

The effect is as follows:

Ideas:

Areas for improvement:

The index. php code is as follows:

123
 456

7. graph perimeter area calculator 8 9 rectangle 10 triangle 11 round 12

13

14 32

3334

The form. class. php code is as follows:

1
 FormAction = $ action; // Save the input parameter to $ formAction; 16 $ this-> shape = isset ($ _ GET ["shape"])? $ _ GET ["shape"]: "rect"; // Obtain the image category from the variables passed in the form. If no image category is transmitted, the default value is 17} 18 function _ toString () {19 $ form = ''; 26 27 return $ form; 28} 29 // private method, returns the input string of the rectangle form. 30 private function getRect () {31 // The input content is displayed after the form is submitted, prevent it from disappearing 32 $ formheight = isset ($ _ POST ['height'])? $ _ POST ['height']: NULL; 33 $ formwidth = isset ($ _ POST ['width'])? $ _ POST ['width']: NULL; 34 $ input ='

Enter the length and width of the rectangle.

'; 35 $ input. =' height of the rectangle:

'; 36 $ input. =' width of the rectangle:
'; 37 return $ input; 38} 39 // return the string 40 private function getTriangle () {41 in the input part of the triangle input form. // The string is displayed after the form is submitted, prevent it from disappearing 42 $ formside1 = isset ($ _ POST ['include1'])? $ _ POST ['include1']: NULL; 43 $ formside2 = isset ($ _ POST ['include2'])? $ _ POST ['side2']: NULL; 44 $ formside3 = isset ($ _ POST ['side3 '])? $ _ POST ['side3 ']: NULL; 45 $ input ='

Enter Three sides of the triangle

'; 46 $ input. =' side length 1:

'; 47 $ input. =' side length 2:

'; 48 $ input. =' side length 3:
'; 49 return $ input; 50} 51 // return the string 52 private function getCircle () in the input part of the circular form () {53 $ formradius = isset ($ _ POST ['radius '])? $ _ POST ['radius ']: NULL; // The content is displayed after the input form is submitted to prevent it from disappearing. 54 $ input ='

Enter the radius

'; 55 $ input. =' radius:
'; 56 return $ input; 57} 58} 59

Result. class. php code:

1
 Shape = new $ _ GET ["shape"] (); 8} 9 // call the attributes and methods of an object, and return the length and area of 10 function _ toString () {11 $ result = $ this-> shape-> shapeName. 'perimeter is '. $ this-> shape-> perimeter ().'
'; 12 $ result. = $ this-> shape-> shapeName.' The area is '. $ this-> shape-> area ().'
'; 13 return $ result; 14} 15}

The abstract class shape. class. php code is as follows:

1
 ShapeName. $ message; 20 echo ''. $ message. 'must be a positive number.
'; 21 return FALSE; 22} 23 else24 return TRUE; 25} 26}

The code of the subclass triangle. class. php is as follows:

1
 ShapeName = "triangle "; // naming convention 17 18 // use the method of the parent class validate to check whether the input is a positive number 19 if ($ this-> validate ($ _ POST ["side1"], "side length 1") & $ this-> validate ($ _ POST ["side2"], "side length 2 ") & $ this-> validate ($ _ POST ["side3"], "side length 3 ")) {20 21 // verify whether the two sides and the third side are greater than 22 if ($ this-> validatesum ($ _ POST ["side1"], $ _ POST ["side2"], $ _ POST ["side3"]) {23 $ this-> side1 = $ _ POST ["side1"]; // if three sides are initialized through verification; 24 $ this-> side2 =$ _ POST ["side2"]; 25 $ this-> side3 =$ _ POST ["side3"]; 26} 27 else {28 echo 'the sum of the two sides must be greater than the third edge'; 29 exit (); 30} 31} 32 else {33 exit (); 34} 35} 36/* Use Helen formula to calculate the area and return the result */37 function area () {38 $ s = ($ _ POST ["side1"] + $ _ POST ["side2"] + $ _ POST ["side3"])/2; 39 return sqrt ($ s * ($ s-$ _ POST ["side1"]) * ($ s-$ _ POST ["side2"]) * ($ s-$ _ POST ["side3"]); 40} 41/* calculates and returns the circumference */42 function perimeter () {43 return $ _ POST ["side1"] + $ _ POST ["side2"] + $ _ POST ["side3"]; 44} 45/* calculate whether the two sides of a triangle are greater than the third side. TRUE is returned; FALSE is returned */46 private function validatesum ($ side1, $ side2, $ side3) {47 if ($ side1 + $ side2)> $ side3 & ($ side1 + $ side3)> $ side2 & ($ side2 + $ side3)> $ side1) 48 return TRUE; 49 else50 return FALSE; 51} 52}

The code for subclass circle. class. php is as follows:

1
 ShapeName = "circle"; 13 if ($ this-> validate ($ _ POST ["radius"], "radius ")) 14 $ this-> radius =$ _ POST ["radius"]; 15} 16 // return area of the circle 17 function area () {18 return 3.14 * $ this-> radius; 19} 20 // returns the circle perimeter 21 function perimeter () {22 return 3.14*2 * $ this-> radius; 23} 24}

The code for subclass rect. class. php is as follows:

1
 ShapeName = "rectangle"; 14 if ($ this-> validate ($ _ POST ["width"], "width ") & $ this-> validate ($ _ POST ["height"], "height") {15 $ this-> width =$ _ POST ["width"]; 16 $ this-> height = $ _ POST ["height"]; 17} 18} 19 // return area 20 function area () {21 return $ this-> width * $ this-> height; 22} 23 // returns the circumference 24 function perimeter () {25 return 2 * ($ this-> width + $ this-> height); 26} 27}

Statement:

1. This article is only applicable to experiments and is not suitable for practical applications. I am not responsible for any adverse consequences.

2. This article is an original blog, which can be freely reproduced on the personal platform. However, you need to indicate the source and attach a link. Otherwise, it will be deemed as theft. It is strictly prohibited to be used for commercial purposes. if necessary, contact me to pay the draft fee and authorize it for use.

This example may not be practical, but basically summarizes the three features of object-oriented: inheritance, encapsulation, polymorphism ....

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: info-contact@alibabacloud.com 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.