The example explains PHP object-oriented polymorphism, and the example explains object-oriented

Source: Internet
Author: User

The example explains PHP object-oriented polymorphism, and the example explains 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
Copy codeThe Code is as follows:
/**
* PHP Polymorphism
* Qiongtai blog
*/
 
// Define the Student Category
Class student {
Public function linoleic (){
Echo "students are attending classes! <Br/> ";
}
}
 
// Define employee class
Class office {
Public function Wor (){
Echo "staff at work! <Br/> ";
}
}
 
// 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.
Copy codeThe 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! <Br/> ";
}
}
 
// Defines the staff class and inherits the public class pub
Class office extends pub {
Public function working (){
Echo "staff at work! <Br/> ";
}
}
 
// 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:
Copy codeThe 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! <Br/> ";
}
}
 
// Define employee class
Class office {
// Define a uniform method pub
Public function pub (){
Echo "staff at work! <Br/> ";
}
}
 
// 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>

Related Article

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.