The instance explains PHP object-oriented polymorphism, and the instance explains object-oriented _ PHP Tutorial

Source: Internet
Author: User
The instance explains the polymorphism of PHP object-oriented, and the instance explains object-oriented. The instance explains PHP object-oriented polymorphism, and the instance explains object-oriented what is polymorphism? Polymorphism is the third feature of object-oriented language after Database abstraction and inheritance. Polymorphism refers to multi-instance explanation of PHP object-oriented polymorphism, and instance explanation of object-oriented

What is polymorphism?

Polymorphism is the third feature of object-oriented language after Database abstraction and inheritance. Polymorphism is a variety of forms, with the ability to express a variety of forms. Objects are processed in different ways based on the object type. Polymorphism allows each object to respond to common messages in a suitable way. Polymorphism enhances software flexibility and reusability.

For example, if we create a doing () method, if it is a student, the class is printed, and if it is a company employee, the work is printed.

Common practice

Use if to judge

The code is as follows:


/**
* PHP polymorphism
* Qiongtai blog
*/

// Define the student category
Class student {
Public function linoleic (){
Echo "students are attending classes!
";
}
}

// Define employee class
Class office {
Public function Wor (){
Echo "staff at work!
";
}
}

// Method for determining object types
Function doing ($ obj ){
If ($ obj instanceof student ){
$ Obj-> linoleic ();
} Elseif ($ obj instanceof office ){
$ Obj-> wor ();
} Else {
Echo "no such object! ";
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work

The above result is output:

Students are attending classes
Staff at work

This common method has a disadvantage, that is, if there are many objects, then if... else... is very long and not flexible.

Polymorphism

Defines a public abstract method, and all subclasses inherit it.

The code is as follows:


/**
* PHP polymorphism
* Qiongtai blog
*/

// Define a public class
Class pub {
Protected function working (){
Echo "this method needs to be reloaded in the subclass! ";
}
}

// Define the student class and inherit from the public class pub
Class student extends pub {
Public function working (){
Echo "students are attending classes!
";
}
}

// Defines the staff class and inherits the public class pub
Class office extends pub {
Public function working (){
Echo "staff at work!
";
}
}

// Method for determining object types
Function doing ($ obj ){
If ($ obj instanceof pub ){
$ Obj-> working ();
} Else {
Echo "no such object! ";
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work


This is the characteristic of polymorphism and flexible reuse.

Other practices

From the implementation of polymorphism, it is nothing more than standardizing a method of the parent class for every class to achieve a unified effect. It is also feasible to add a unified method when defining classes. The preceding example can also be implemented as follows:

The code is as follows:


/**
* PHP polymorphism
* Qiongtai blog
*/

// Define the student category
Class student {
// Define a uniform method pub
Public function pub (){
Echo "students are attending classes!
";
}
}

// Define employee class
Class office {
// Define a uniform method pub
Public function pub (){
Echo "staff at work!
";
}
}

// Method for determining object types
Function doing ($ obj ){
If ($ obj ){
// Call the unified method of the class
$ Obj-> pub ();
} Else {
Echo 'no such object ';
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work


Of course, the above examples do not show that polymorphism can be done in this way. after all, polymorphism can effectively achieve flexible reuse in complex programming.

Polymorphism can also be understood as a programming method, and the ultimate goal of programming is flexibility, polymorphism, reuse, and efficiency.


What is object-oriented polymorphism?

The concept of object-oriented programming has always been pursued by programmers. Previous programming was process-oriented and cannot be used for large-scale projects. object-oriented programming is a revolution. it is more suitable for people's thoughts and thoughts, rather than programming based on machine language ideas! In C #, everything is an object! In fact, it is not hard to understand the object. for example, if you are looking for a wife, what is your wife, how old is it, what's your surname, what's your job, and how many boyfriends have you made, and so on.

When talking about type conversion, this should be needless to say!
To forcibly convert a parent class object to a subclass object, the precondition is that the parent class object has a subclass object value, that is, the subclass object has been assigned to the parent class variable!
///
If the parent class object is converted to a subclass object, is the method called the parent class or subclass (or attribute )?

This is hard to say. it may take some time to make it clear! There are many questions, such as whether to hide, override, and parent class methods in the subclass. Besides, whether the parent class is an abstract class or an interface should be analyzed in detail!
It is best to paste your code to help you analyze it!

The above is my understanding. if it is incorrect, please try again!

Understanding of polymorphism in object orientation

In simple terms, polymorphism is a feature with the ability to express multiple forms. in OO, it means that a language can be processed in different ways according to the object type, especially the ability to overload methods and inheritance classes. Polymorphism is considered an essential feature of object-oriented languages.

There are multiple types of polymorphism. by understanding these types, you can fully understand them and will not list them here. please refer to Wikipedia and javaworld.

Polymorphism and generic (generic)

Polymorphism is actually generic.

Generic encoding is a general encoding method for different types, regardless of the data result or algorithm.

The traditional generic model refers to generalization of parameters similar to Template functions. Typical applications are C ++ STL, such as List, Vector, and algorithm.

OO has been able to perform generic operations in the real sense through interfaces and Abstract classes. In my opinion, this is the most exciting part of OO, that is, the power of polymorphism. In the traditional sense, I have always felt that the role of Generic is not as effective as ever.

Inheritance)

Strictly speaking, polymorphism is not isolated from inheritance and overloading, and there is a close relationship between them, polymorphism is based on the two (in fact, inheritance is useful for overloading ).

Traditional polymorphism is actually implemented by Virtual functions using Virtual tables (the most commonly used in early C simulation of OO features, and the implementation of C ++ is also true, subsequent technology is not studied, and it is not known whether to use VT). Naturally, inheritance is inseparable. In other words, polymorphism actually overwrites inheritance.

It is precisely because of the close relationship between inheritance and polymorphism that it is easy for us to distinguish between them?

For example:

Abstract Class Sharp implement IHaveSide {
Public bool isSharp (){
Return true;
}
Public abstract int getSides ();
}

Class Triangle extends Sharp {
Public override int getSides (){
Return 3;
}
}

Class Rectangle extends Sharp {
Pubilc override int getSides (){
Return 4;
}
}

The relationships of these types are called Inheritance. the following method of use also brings about inheritance:
Triangel tri = new Triangle ();
Println ("Triangle is a type of sharp? "+ Tri. isSharp ());

This method is Polymorphism:
Sharp sharp = new Rectangle ();
Println ("My sharp has" + sharp. getSides () + "sides .");

What are the differences between the two? Obviously, inheritance is the method in which child classes use parent classes, while polymorphism is the method in which child classes use child classes.

The technical difference is that in the binding period, the binding in the later stage must be polymorphism .... Remaining full text>

What is polymorphism? Polymorphism is the third feature of object-oriented language after Database abstraction and inheritance. Polymorphism is multi...

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.