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 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 (taking up space but does not work), it is different from a normal method - 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 from them and then instantiate Subclass. You can also think of an abstract class as a template for a subclass.
If you override all abstract methods, the subclass becomes a normal class. If you do not override all methods, Subclasses are 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.
Declare abstract methods The syntax is different from that of declaring a general method. The abstract method does not have the main body contained in braces {} like the general method, and is ended with a semicolon;
In the following program file, we define a method containing getArea method of class Shape. But since it is impossible to determine the area of the figure without knowing the shape, indeed we 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, just Like in Example 6.13.
If 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 classes and implements instead of extends to specify your class definition or use an interface. For example, you can write a myClass implements myIterface. You can choose between these two methods based on personal preference.
Code
Copy code The code is as follows:
/*Note:
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)
*/
//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->base * $this->height)/2);
}
public function getNumberOfSides() //Override the side count method
{
return(3);
}
}
//concrete class entity 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 entity class 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 it into 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) print("Area: ".$s->getArea()."
n"); //If $s is an instance of the Shape class
if($s instanceof Polygon ) print("Sides: ".$s->getNumberOfSides()."
n");
if($s instanceof Color) print("Color:".$s->name. "
n");
print("
n");
}
?>
Run result:
Area: 35
Sides: 4
Area: 10
Sides: 3
Area: 28.274333882308
Color:blue
http://www.bkjia.com/PHPjc/321175.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321175.htmlTechArticleOf 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 I...