Php object-oriented full strategy (15th) application of polymorphism
Source: Internet
Author: User
Polymorphism is one of the three main characteristics of object-oriented programming in addition to encapsulation and inheritance. in my opinion, although polymorphism can be achieved in PHP, it is different from object-oriented programming languages such as C ++ and Java, polymorphism is not so prominent
Polymorphism is one of the three major object-oriented features except encapsulation and inheritance. in my opinion, although
To achieve polymorphism, but compared with the object-oriented languages C ++ and Java, polymorphism is not so prominent, because PHP
It is a weak type language, and no parent class object is converted into a subclass object or a subclass object into a parent class object.
So the application of polymorphism is not so obvious. The so-called polymorphism means that a program can process multiple types
The ability of the target, for example, to work in a company, to pay a monthly financial account, the same way of sending a salary, not in the company
The same employees or employees in different positions are issued through this method, but their salaries are different.
Therefore, there are many forms of the same wage payment method. For object-oriented programs, Polymorphism refers
It is like assigning a value to the parent class reference, and then calling the method of the parent class to execute the method that the subclass overrides the parent class, but in PHP it is
Weak type, object references are the same, regardless of parent class references or subclass references.
Now let's look at an example. to use polymorphism, there must be a relationship between the parent class object and the subclass object. Do 1
Shape interfaces or abstract classes as parent classes, which have two abstract methods, one for perimeter and the other for perimeter
Area method; this interface subclass is a variety of different shapes, each shape has a length and area, and because the parent class is
An interface, so the child class must implement the abstract methods of the two weeks and areas of the parent class.
Is that child classes of different shapes comply with the parent class interface specifications, there must be a method for calculating the circumference and area.
Code snippet
The code is as follows:
// Defines a shape interface, which has two abstract methods for sub-classes to implement
Interface Shape {
Function area ();
Function perimeter ();
}
// Defines a rectangular subclass to implement the circumference and area of 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 to implement the circumference and area of the shape interface.
Class Circular implements Shape {
Private $ radius;
Function _ construct ($ radius ){
$ This-> radius = $ radius;
}
Function area (){
Return "the circular area is:". (3.14 * $ this-> radius );
}
Function perimeter (){
Return "the circumference of the 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 circular object of the subclass to a reference of the shape.
$ Shape = new Circular (10 );
Echo $ shape-> area ()."
";
Echo $ shape-> perimeter ()."
";
?>
Result of the preceding example:
Execution result
The area of the rectangle is: 50.
The circumference of the rectangle is: 30.
Circular area: 314
The circle circumference is: 62.8
In the above example, we can see that the rectangle object and the circle object are assigned to the variable $ shape, and the $ shape reference is called.
The area and perimeter of the method, there are different results, this is a kind of polymorphism application, in fact, in our PHP such weak
In the object-oriented language of the class, the characteristics of polymorphism are not particularly obvious. In fact, it is the variable of the object type.
Application.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service