Application of polymorphism
Polymorphism is another one of the three major features of object-oriented besides encapsulation and inheritance. In my personal opinion, although polymorphism can be achieved in PHP, it is different from C++ and Java. Compared with these object-oriented languages, polymorphism is not so prominent, because PHP
itself is a weakly typed language. There is no conversion of parent class objects into subclass objects or subclass objects into parent class objects.
object problem, so the application of polymorphism is not so obvious; the so-called polymorphism refers to the ability of a program to handle multiple types of
objects. For example, when working in a company, the financial department pays wages every month , the same salary payment method, different employees or employees in different positions within the company are all paid through this method, but the wages paid are different.
So the same method of paying wages appears in many forms. For object-oriented programs, polymorphism is to assign the subclass object
to the parent class reference, and then call the method of the parent class to execute the method of the subclass overriding the parent class, but in PHP it is
Weakly typed, object references are the same regardless of parent class reference or subclass reference.
Let’s look at an example now. First of all, to use polymorphism, there must be a relationship between parent class objects and subclass objects. Make a
shape interface or abstract class as the parent class. There are two abstract methods in it, one is to find the perimeter, and the other is to find the
area; there are many subclasses of this interface Different shapes, each shape has a perimeter and an area, and because the parent class is an interface, the subclass must implement the two abstract methods of the parent class's perimeter and area, so The purpose
is that each subclass of different shapes must comply with the specifications of the parent class interface and have methods for calculating perimeter and area.
Code snippet
Copy code
The code is as follows:
//Defines a shape Interface, there are two abstract methods in it for subclasses to implement
interface Shape{
function area();
function perimeter();
}
//Defines a rectangular subclass Implements the perimeter and area in the shape interface
class Rect implements Shape{
private $width;
private $height;
function __construct($width, $height){
$this ->width=$width;
$this->height=$height;
}
function area(){
return "The area of the rectangle is:".($this-> ;width*$this->height);
}
function perimeter(){
return "The perimeter of the rectangle is:".(2*($this->width+$this-> ;height));
}
}
//Defines a circular subclass that implements the perimeter and area in the shape interface
class Circular implements Shape{
private $radius;
function __construct($radius){
$this->radius=$radius;
}
function area(){
return "The area of the circle is:".(3.14 *$this->radius*$this->radius);
}
function perimeter(){
return "The perimeter of a circle is:".(2*3.14*$this- >radius);
}
}
//Assign the subclass rectangle object to a reference of the shape
$shape=new Rect(5, 10);
echo $shape- >area()."
";
echo $shape->perimeter()."
";
//Assign the subclass circular object to a reference of the shape
$shape=new Circular(10);
echo $shape->area()."
";
echo $shape->perimeter()."
";
?>
Execution result of the above example:
Execution result
The area of the rectangle is: 50
The perimeter of the rectangle is: 30
Circle The area of the shape is: 314
The perimeter of the circle is: 62.8
Through the above example, we can see that the rectangular object and the circular object are assigned to the variable $shape respectively, and the $shape reference is called
The area and perimeter methods have produced different results. This is a polymorphic application. In fact, in our weak
type object-oriented language like PHP, the polymorphic feature is not special. Obviously, it is actually the variable
application of the object type variable.
http://www.bkjia.com/PHPjc/320620.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320620.htmlTechArticleApplication of Polymorphism Polymorphism is another one of the three major characteristics of object-oriented in addition to encapsulation and inheritance. , In my personal opinion, although polymorphism can be achieved in PHP, it is not as object-oriented as C++ and Java...