Section 10 abstract methods and abstract classes [10]

Source: Internet
Author: User
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
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
// Abstract root class
Abstract class Shape
{
Abstract function getArea (); file: // defines an abstract method
}

// Abstract child class abstract subclass
Abstract class Polygon extends Shape file: // Polygon
{
Abstract function getNumberOfSides ();
}

// Concrete class entity class triangle class
Class Triangle extends Polygon
{
Public $ base;
Public $ height;

Public function getArea () file: // overwrite the calculation area method.
{
Return ($ this-> base * $ this-> height)/2 );
}

Public function getNumberOfSides () file: // overwrite edge count 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 (); file: // create a collection of shapes and place 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) file: // 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 ");
}

?>

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.