Abstract method and abstract class _php foundation of PHP5.0 object model exploration

Source: Internet
Author: User
Tags class definition object model
Object-oriented programs are constructed from a hierarchical structure of classes, and in a single inheritance language such as PHP, the inheritance of classes is tree-like. A root class has one or more subclasses, and then inherits one or more next-level subclasses from each subclass. Of course, there may be multiple root classes that can be used to implement different functions. In a well-designed system, each root class should have a useful interface that can be used by the application code. If our application code is designed to work with the root class, it can also collaborate with any subclass that inherits from the Root class.

An abstract method is like a placeholder for a generic method in a subclass (which takes place but does not work) and differs from the general method-there is no code. If there is one or more abstract methods in the class, the class becomes an abstract class. You cannot instantiate abstract classes. You have to inherit them and then instantiate subclasses, and you can also think of abstract classes as a template for subclasses.

If you override all of the abstract methods, the subclass becomes an ordinary class. If you do not overwrite all methods, subclasses are still abstract. If there is an abstract method in a class (even if there is only one), you must declare that the class is abstract, plus abstract before the class keyword.

The syntax for declaring an abstract method differs from declaring a generic method, which is not contained in the main part of the brace {} as a general method, and ends with a semicolon;

In example 6.13, we define a class shape that contains a Getarea method. But since we do not know the shape is not possible to determine the area of the graph, we have declared that the Getarea method is 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 that has only an abstract method, you define an interface (interface). To illustrate this, there are interface and implements keywords in PHP. You can use interface instead of abstract classes, using implements instead of extends to illustrate your class definition or use an interface, for example, you can write a MyClass implements Myiterface. These two methods can be chosen on the basis of personal preference.

* Note:
Two methods refer to:
1. Abstract class aaa{} (Note that there are only abstract methods in AAA, there is no general method)
Class BBB extends aaa{} (the abstract method of override AAA in BBB)
2. Interface aaa{}
Class BBB implements aaa{} (the abstract method of override AAA in BBB)
*/

Listing 6.13 Abstract Classes

Abstract root class abstraction roots
Abstract class Shape
{
Abstract function Getarea (); Define an abstract method
}

Abstract Child class abstraction subclass
Abstract class Polygon extends Shape//polygon
{
Abstract function getnumberofsides ();
}

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

Public Function Getarea ()//Overwrite calculated area method
{
Return (($this->base * $this->height)/2);
}

Public Function Getnumberofsides ()///Overwrite edge count statistic method
{
return (3);
}
}

Concrete class Solid Quadrilateral
Class Rectangle extends Polygon
{
Public $width;
Public $height;

Public Function Getarea ()
{
Return ($this->width * $this->height);
}

Public Function Getnumberofsides ()
{
return (4);
}
}

Concrete Class Entity Circle
Class Circle extends Shape
{
Public $radius;

Public Function Getarea ()
{
Return (PI () * $this->radius * $this->radius);
}
}

Concrete root class defines a color class
Class Color
{
Public $name;
}

$myCollection = Array (); Create a collection 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");
}

? >

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.