Home  >  Article  >  Backend Development  >  Detailed explanation of php abstract class code examples

Detailed explanation of php abstract class code examples

伊谢尔伦
伊谢尔伦Original
2017-07-03 09:33:081154browse

Object-orientedPrograms are built through a hierarchical structure of classes. In single inheritance languages ​​such as PHP, class inheritance is tree-like. A root class has one or more subclasses, Then inherit one or more lower-level subclasses from each subclass.

Of course, there may be multiple root classes to implement different functions. In a well-designed system, each root class Classes should have a useful interface that can be used by application code. If our application code is designed to work with the root class, then it can also work with any subclass that inherits from the root class.

Abstract methods are just like placeholders for general methods in subclasses (take up a place but do not work). They are different from general methods - they do not have any code. If there are one or more abstract methods in the class, then This class becomes Abstract Class. You cannot instantiate abstract classes. You must inherit them and then instantiate subclasses. You can also think of abstract classes as a template for subclasses.

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 only one) , you must declare that this class is abstract, add abstract before the class keyword.

The syntax for declaring abstract methods is different from declaring general methods. Abstract methods are not enclosed in curly brackets {} like general methods. in the main part, and ends with a semicolon;
In the following program file, we define a class Shape that contains the getArea method. However, since it is impossible to determine the area of ​​the figure without knowing the shape, we indeed declare The getArea method is 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 create If you define 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 extends to declare your class definition or use an interface. For example, you can write a myClass implements myIterface. Either method can be chosen according to personal preference.

Code

/*注: 
两种方法即指: 
1. abstract class aaa{} (注意aaa中只有抽象方法,没有一般方法) 
class bbb extends aaa{} (在bbb中覆写aaa中的抽象方法) 
2. interface aaa{} 
class bbb implements aaa{} (在bbb中覆写aaa中的抽象方法) 
*/ 
<?php 
//abstract root class 抽象根类 
abstract class Shape 
{ 
abstract function getArea(); //定义一个抽象方法 
} 
//abstract child class 抽象子类 
abstract class Polygon extends Shape //多边形 
{ 
abstract function getNumberOfSides(); 
} 
//concrete class 实体类 三角形类 
class Triangle extends Polygon 
{ 
public $base; 
public $height; 
public function getArea() //覆写计算面积方法 
{ 
return(($this->base * $this->height)/2); 
} 
public function getNumberOfSides() //覆写边数统计方法 
{ 
return(3); 
} 
} 
//concrete class 实体类四边形 
class Rectangle extends Polygon 
{ 
public $width; 
public $height; 
public function getArea() 
{ 
return($this->width * $this->height); 
} 
public function getNumberOfSides() 
{ 
return(4); 
} 
} 
//concrete class 实体类 圆形 
class Circle extends Shape 
{ 
public $radius; 
public function getArea() 
{ 
return(pi() * $this->radius * $this->radius); 
} 
} 
//concrete root class 定义一个颜色类 
class Color 
{ 
public $name; 
} 
$myCollection = 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) print("Area: ".$s->getArea()."<br>\n"); //如果$s是Shape类的实例 
if($s instanceof Polygon) print("Sides: ".$s->getNumberOfSides()."<br>\n"); 
if($s instanceof Color) print("Color:".$s->name."<br>\n"); 
print("<br>\n"); 
} 
?>

Run Result:

Area: 35 
Sides: 4 
Area: 10 
Sides: 3 
Area: 28.274333882308 
Color:blue

The above is the detailed content of Detailed explanation of php abstract class code examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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