Introduction and advantages of class polymorphism 1 polymorphism. 2 operator: instanceof. 3. simple application of polymorphism. 1 Introduction and advantages of polymorphism: Polymorphism inherits the abstract and inheritance of object-oriented language
Class polymorphism
1. introduction and advantages of polymorphism.
2. OPERATOR: instanceof.
3. simple application of polymorphism.
1. introduction and advantages of polymorphism
Introduction: polymorphism is the third feature of object-oriented language after being abstracted and inherited.
For example, the USB interface uses different functions to insert different things.
Advantage: OOP not only combines many functions and functions, but describes a situation in our life using classes, inheritance, and polymorphism.
2. OPERATOR: instanceof
PHP is a type operator used to determine whether a given object comes from a specified object.
The format code is as follows:
- Class {}
- Class B {}
- $ Thing = new;
- If ($ thing instanceof ){
- Echo "";
- }
- If ($ thing instanceof B ){
- Echo "B ";
- }
3. simple application of polymorphism
The code for instance 1 is as follows:
- Class {
- }
- Class B {
- }
- $ New = new;
- If ($ new instanceof ){
- Echo "";
- }
- If ($ new instanceof B ){
- Echo "B ";
- }
- ?>
The code for instance 2 is as follows:
- Interface MyUsb {
- Function type ();
- Function alert ();
- }
- Class Zip implements MyUsb {
- Function type (){
- Echo & quot; 2.0 & quot ";
- }
- Function alert (){
- Echo "the USB flash drive is detecting ...... ";
- }
- }
- Class Mp3 implements MyUsb {
- Function type (){
- Echo & quot; 1.0 & quot ";
- }
- Function alert (){
- Echo "the MP3 driver is detecting ...... ";
- }
- }
- Class MyPc {
- Function Add_Usb ($ what ){
- $ What-> type ();
- $ What-> alert ();
- }
- }
- $ P = new MyPc ();
- $ Zip = new Zip ();
- $ Mp3 = new Mp3 ();
- $ P-> Add_Usb ($ zip );
- $ P-> Add_Usb ($ mp3 );
- ?>
Add an instance code:
-
-
- Inheritance and polymorphism
-
-
- /* Parent class */
- Class MyObject {
- Public $ object_name; // book name
- Public $ object_price; // book price
- Public $ object_num; // number of books
- Public $ object_agio; // book discount
- Function _ construct ($ name, $ price, $ num, $ agio) {// Constructor
- $ This-> object_name = $ name;
- $ This-> object_price = $ price;
- $ This-> object_num = $ num;
- $ This-> object_agio = $ agio;
- }
- Function showMe () {// output function
- Echo 'is not displayed. ';
- }
- }
- /* Subclass Book */
- Class Book extends MyObject {// subclass of MyObject.
- Public $ book_type; // type
- Function _ construct ($ type, $ num) {// declare the constructor
- $ This-> book_type = $ type;
- $ This-> object_num = $ num;
- }
- Function showMe () {// override the showMe method in the parent class
- Return 'This New'. $ this-> book_type. 'book'. $ this-> object_num .'
';
- }
- }
- /* Subclass Elec */
- Class Elec extends MyObject {// another subclass of MyObject
- Function showMe () {// override the showMe method in the parent class
- Return 'hot books: '. $ this-> object_name .'
Original price: '. $ this-> object_price .'
Special offer: '. $ this-> object_price * $ this-> object_agio;
- }
- }
- /* Instantiate the object */
- $ C_book = new Book ('computer class', 1000); // declare a Book subclass object
- $ H_elec = new Elec ('php function reference ', 0.8,); // declare an Elec subclass object
- Echo $ c_book-> showMe ()."
"; // Output the showMe () method of the Book subclass.
- Echo $ h_elec-> showMe (); // The showMe () method is used to output the Elec subclass.
- ?>
-
-