Section 10 Abstract Methods and Abstract Classes [10]_PHP Tutorial

WBOY
Release: 2016-07-21 16:10:45
Original
822 people have browsed it


Object-oriented programs 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, and each subclass inherits one or more More lower-level subclasses. 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 to work with the root class, it can also work with any subclass that inherits from the root class.

Abstract methods are just like regular methods in subclasses A placeholder (takes up a place but does not work), which is different from a general method - it does not have any code. If one or more abstract methods exist in a class, then the class becomes an abstract class. You cannot instantiate an abstract class. You must inherit them and then instantiate the subclass. You can also think of the abstract class as a template for the subclass.

If you override all abstract methods, the subclass becomes a normal class . If all methods are not overridden, 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 of declaring an abstract method is different from that of declaring a general method. The abstract method does not have a main body contained in braces {} like a general method, and ends with a semicolon;.

In the example In 6.13, we defined a class Shape that contains a getArea method. But since it is impossible to determine the area of ​​the figure without knowing the shape, 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, as in Example 6.13.

If you create a class with only abstract methods, you define an interface. To illustrate this situation, PHP There are interface and implements keywords. You can use interface instead of abstract class, and implements instead of extends to describe your class definition or use an interface. For example, you can write a myClass implements myIterface. These two methods can be customized according to your needs. Choose according to your 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{} (override the abstract method in aaa in bbb)
2. interface aaa{}
class bbb implements aaa{} (overwrite the abstract method in aaa in bbb)
* /

Listing 6.13 Abstract classes
//abstract root class abstract root class
abstract class Shape
{
abstract function getArea(); file://define an abstract method
}

//abstract child class Abstract subclass
abstract class Polygon extends Shape file://Polygon
{
abstract function getNumberOfSides();
}

//concrete class Entity class Triangle class
class Triangle extends Polygon
{
public $base;
public $height;

public function getArea() file://override area calculation method
{
return(($this->base * $this->height)/2);
}

public function getNumberOfSides() file://Overwrite the number of sides Statistical method
{
return(3);
}
}

//concrete class entity class Rectangle
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(); file://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) file://if $s is an instance of 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");
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314128.htmlTechArticleObject-oriented programs are built through a hierarchical structure of classes. In single inheritance languages ​​such as PHP, class inheritance It is tree-like. A root class has one or more subclasses, and then inherits a class from each subclass...
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