Section 10--Abstract methods and abstract classes_PHP tutorial

WBOY
Release: 2016-07-21 16:01:05
Original
728 people have browsed it

/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 10--Abstract methods and abstract classes
Object-oriented programming through the hierarchical structure of classes Build it up. In single inheritance languages ​​such as PHP, class inheritance is tree-like. A root class has one or more subclasses, and each subclass inherits one or more subclasses. Of course. , there may be multiple root classes used 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 If it works with the root class, it can also work with any subclass that inherits from the root class.
Abstract methods are placeholders for general methods in subclasses (take up space but do not work) , which is different from general methods - there is no code. If one or more abstract methods exist in a class, then the class becomes an abstract class. You cannot instantiate abstract classes. You must inherit them and then instantiate subclasses. You You can also think of an abstract class as a template for a subclass.
If you override all abstract methods, the subclass becomes an ordinary class. If you do not override all methods, the subclass is still abstract. If a class contains an abstract method (even if there is only one), you must declare the class to be abstract by adding abstract before the class keyword.
The syntax for declaring abstract methods is different from declaring general methods. The syntax of abstract methods There is no body part enclosed in curly braces {} like a normal method, and ended with a semicolon;.
In Example 6.13, we defined a class Shape that contains the getArea method. But it is impossible because we don’t know the shape. To determine the area of ​​the shape, we declared the getArea method as abstract. You cannot instantiate a Shape object, but you can inherit from it or use it in an expression, as in Example 6.13.
If you When you create a class with only abstract methods, you define an interface. To illustrate this situation, PHP has the interface and implements keywords. You can use interface instead of abstract class and implements instead of extends to illustrate this. Your class defines or uses an interface. For example, you can write a myClass implements myIterface. These two methods can be chosen according to personal preference.
/*Note:
The two methods refer to:
1 . abstract class aaa{} (note that there are only abstract methods in aaa, no general methods)
class bbb extends aaa{} (overwrite the abstract methods in aaa in bbb)
2. interface aaa{}
class bbb implements aaa{} (override abstract methods in aaa in bbb)
*/
Listing 6.13 Abstract classes

Copy codeThe code is as follows:
//abstract root class 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 class
class Triangle extends Polygon
{ public $base;
public $height;
public function getArea() //Override the area calculation method
{          return(($this- & gt; base*$ this- & gt; height)/2);
}
Public function getnumberofsides () // The statistical method of overwriting edges
{
Return (3);

 } 
 //concrete class Solid class Polygon 
 class Rectangle extends Polygon 
 { 
 public $width; 
 public $height; > Public function getArea()
                                                                                                                           return(4);                                            
}
//concrete class entity class Circle
class Circle extends Shape
{
public $radius;
public function getArea()
                                                                                                   pi() * $this->radius * $this->radius);
} }
}
//concrete root class defines a color class > Public $name;
}
$myCollection = array(); //Create a collection of shapes and put it into 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 Shape class
{
print("Area: ". $s->getArea().
                                                                        
$s->getNumberOfSides( ) .                                                   $s->name
n ");
}
print("
n");
}
?>




http://www.bkjia.com/PHPjc/316938.html

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/316938.html

TechArticle/* +--------------------- -------------------------------------------------- --------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translation is mainly + personal...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!