Section Tenth-abstract method and abstract class _php Foundation

Source: Internet
Author: User
Tags abstract class definition inheritance php programming
/*
+-------------------------------------------------------------------------------+
| = This article is for Haohappy read <<core PHP programming>>
| = Notes from the chapter classes and objects
| = translation-oriented + personal experience
| = Please do not reprint to avoid any unnecessary trouble that may occur, thank you
| = Welcome to criticize, hope and all PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section Tenth-abstract methods and abstract classes
Object-oriented programs are constructed by hierarchical structure of classes. In a single inheritance language such as PHP, the inheritance of a class 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 must inherit them and then instantiate the subclass. 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 is different from declaring a generic method. The abstract method is not contained in the main part of the brace {} as the 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
Copy Code code as follows:
<?php
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 ().
"<br>\n");
}
if ($s instanceof Polygon)
{
Print ("Sides:").
$s->getnumberofsides ().
"<br>\n");
}
if ($s instanceof Color)
{
Print ("Color: $s->name<br>\n");
}
Print ("<br>\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.