Section 10 -- abstract methods and abstract Classes -- Classes and Objects in PHP5 [10]

Source: Internet
Author: User

Section 10-abstract methods and abstract classes

Object-oriented Programs are built through the hierarchical structure of classes. in a single inheritance language such as PHP, class inheritance is tree-like. A root class has one or more sub-classes, and then inherits one or more sub-classes from each sub-class. of course, there may be multiple root classes to implement different functions. in a well-designed system, each root class should have a useful interface that can be used by application code. if our application code is designed to work with the root class, it can also work with any subclass inherited from the root class.

An abstract method is like a placeholder for a method in a subclass (which occupies a place but does not work). It is different from a general method-there is no code. if one or more abstract methods exist in the class, the class becomes an abstract class. you cannot instantiate an abstract class. you must inherit them and instantiate them. you can also regard the abstract class as a template of a subclass.

If you override all the abstract methods, the subclass will become a common class. if no method is overwritten, The subclass is still abstract. if a class contains an abstract method (even if there is only one), you must declare that the class is abstract and add abstract before the class keyword.

The syntax for declaring an abstract method is different from that for declaring a general method. The abstract method does not include the subject part in braces {} as the general method, and ends with a semicolon.

In Example 6.13, we define a Shape class containing the getArea method. however, since we do not know the shape, it is impossible to determine the area of the image, we have declared the getArea method as an abstract method. you cannot instantiate a Shape object, but you can inherit it or use it in an expression, as in example 6.13.

If you create a class with only abstract methods, you define an interface ). to illustrate this situation, PHP has the keyword "interface" and "implements. you can use interfaces instead of abstract classes, and implements instead of extends to describe your class or use an interface. for example, you can write a myClass implements myIterface. these two methods can be selected based on personal preferences.

/* Note:
Two methods refer:
1. abstract class aaa {} (note that aaa only contains abstract methods, but does not have general methods)
Class bbb extends aaa {} (override the abstract method in aaa in bbb)
2. interface aaa {}
Class bbb implements aaa {} (override the abstract method in aaa in bbb)
*/

Listing 6.13 Abstract classes

<? Php // abstract root class abstract class Shape {abstract function getArea (); // define an abstract method} // abstract child class abstract subclass abstract class Polygon extends Shape // Polygon {abstract function getNumberOfSides ();} // concrete class entity class Triangle extends Polygon {public $ base; public $ height; public function getArea () // override the calculated Area Method {return ($ this-> base * $ this-> height)/2);} public function getNu MberOfSides () // override Edge Number statistical method {return (3) ;}/// concrete class Object class quadrilateral class Rectangle extends Polygon {public $ width; public $ height; public function getArea () {return ($ this-> width * $ this-> height);} public function getNumberOfSides () {return (4 );}} // concrete class Object class circular class Circle extends Shape {public $ radius; public function getArea () {return (pi () * $ this-> radius );}}// Concrete root class defines a Color class Color {public $ name ;}$ myCollection = array (); // creates a set of shapes, put the array // make a rectangle $ r = new Rectangle; $ r-> width = 5; $ r-> height = 7; $ myCollection [] = $ r; unset ($ r); // make a triangle $ t = new Triangle; $ t-> base = 4; $ t-> height = 5; $ myCollection [] = $ t; unset ($ t); // make a circle $ c = new Circle; $ c-> radius = 3; $ myCollection [] = $ c; unset ($ c); // make a color $ c = New Color; $ c-> name = "blue"; $ myCollection [] = $ c; unset ($ c); foreach ($ myCollection as $ s) {if ($ s instanceof Shape) // if $ s is an instance of the Shape class {print ("Area :". $ s-> getArea (). "<br>");} if ($ s instanceof Polygon) {print ("Sides :". $ s-> getNumberOfSides (). "<br>");} if ($ s instanceof Color) {print ("Color: $ s-> name <br> ");} print ("<br>") ;}?>

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.