PHP polymorphic, PHP polymorphic
1. What is polymorphic
Polymorphism (polymorphism) is literally understood as "multiple shapes." Can be understood as a variety of manifestations, also known as "an external interface, a number of internal implementation methods." In object-oriented theory, the general definition of polymorphism is that the same operation acts on different instances of the class, resulting in different execution results. That is, when objects of different classes receive the same message, they will get different results.
In the actual application development, the use of multi-state in object-oriented is mainly that the different sub-class objects can be treated as a parent class, and can block the differences between different sub-class objects, write out common code, and make general programming to adapt to the changing requirements.
/**
* Shape Interface
*
* @version 1.0
* @copyright
* (1) with Interface (interface), you can specify which methods a class must implement, but do not need to define the specifics of these methods.
* (2) We can define an interface by interface, just like defining a standard class, but all the methods defined in it are empty.
* (3) All methods defined in the interface must be public, which is the attribute of the interface
*/
Interface Shape {
Public function Draw ();
}
/**
* Triangle
*
* @uses Shape
* @version 1.0
* @copyright
* (1) To implement an interface, you can use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported.
* (2) If you want to implement multiple interfaces, you can use commas to separate the names of multiple interfaces.
*/
Class Triangle implements Shape {
Public Function Draw () {
Print "Triangle::d raw () \ n";
}
}
/**
* Rectangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
Class Rectangle implements Shape {
Public Function Draw () {
Print "Rectangle::d raw () \ n";
}
}
/**
* Test polymorphism
*
* @version 1.0
* @copyright
*/
Class Testpoly {
Public Function Drawnow ($shape) {
$shape->draw ();
}
}
$test = new Testpoly ();
$test->drawnow (New Triangle ());
$test->drawnow (New Rectangle ());
?>
http://www.bkjia.com/PHPjc/1119060.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119060.html techarticle php polymorphic, PHP polymorphic 1. What is polymorphic polymorphism (polymorphism) is literally understood as multiple shapes. Can be understood as a variety of manifestations, also known as an external interface, multiple inside ...