Section 10-abstract methods and abstract classes /*
+ ------------------------------------------------------------------------------- +
| = This article is Haohappy read < >
| = Notes in the Classes and Objects chapter
| = Translation-oriented + personal experiences
| = Do not repost it to avoid unnecessary troubles. thank you.
| = Thank you for your criticism and hope to make progress together with all PHP fans!
| = PHP5 site: http://blog.csdn.net/haohappy2004
+ ------------------------------------------------------------------------------- +
*/
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
The code is as follows:
// Abstract root class
Abstract class Shape
{
Abstract function getArea (); // defines an abstract method
}
// Abstract child class abstract subclass
Abstract class Polygon extends Shape // Polygon
{
Abstract function getNumberOfSides ();
}
// Concrete class entity class triangle class
Class Triangle extends Polygon
{
Public $ base;
Public $ height;
Public function getArea () // overwrite the calculation area method
{
Return ($ this-> base * $ this-> height)/2 );
}
Public function getNumberOfSides () // override edge number statistics 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 Circle
Class Circle extends Shape
{
Public $ radius;
Public function getArea ()
{
Return (pi () * $ this-> radius );
}
}
// Concrete root class defines a color class
Class Color
{
Public $ name;
}
$ MyCollection = array (); // create a set of shapes and put them in an 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 ().
"
\ N ");
}
If ($ s instanceof Polygon)
{
Print ("Sides :".
$ S-> getNumberOfSides ().
"
\ N ");
}
If ($ s instanceof Color)
{
Print ("Color: $ s-> name
\ N ");
}
Print ("
\ N ");
}
?>