Examples of PHP object-oriented polymorphism, examples of object-oriented
What is polymorphism?
Polymorphism is the third feature of object-oriented language following the abstraction and inheritance of database. Polymorphism is a variety of forms, with the ability to express a variety of forms of characteristics. Representation in an object-oriented manner is handled differently depending on the type of object. Polymorphism allows each object to respond to a common message in a way that suits itself. Polymorphism enhances the flexibility and reusability of software.
If we create a doing () method, if the student prints the class, the company clerk prints to work.
General Practice
Use if to determine
Copy the Code code as follows:
/**
* PHP polymorphism
* Jones Taiwan Blog
*/
Defining student Classes
Class student{
Public Function cla () {
echo "Student work is in Class!"
";
}
}
Define the Employee class
Class office{
Public Function Wor () {
echo "Clerk is at work!
";
}
}
Judging Object Type methods
function doing ($obj) {
if ($obj instanceof student) {
$obj->cla ();
}elseif ($obj instanceof Office) {
$obj->wor ();
}else{
echo "No this object!" ";
}
}
Doing (new student ()); The students are having a class
Doing (new Office ()); The clerk is at work
Above results output:
The students are having a class
The clerk is at work
This common method has a disadvantage, that is, if a lot of objects, then if. else.. Very long and inflexible.
Polymorphism practices
Defines a public abstract method that inherits it from all subclasses.
Copy the Code code as follows:
/**
* PHP polymorphism
* Jones Taiwan Blog
*/
Define a public class
Class pub{
protected function Working () {
echo "This method needs to be overloaded in subclasses!";
}
}
Define student class, Inherit public class pub
Class Student extends pub{
Public Function working () {
echo "Student work is in Class!"
";
}
}
Define employee class, Inherit public class pub
Class Office extends pub{
Public Function working () {
echo "Clerk is at work!
";
}
}
Judging Object Type methods
function doing ($obj) {
if ($obj instanceof Pub) {
$obj->working ();
}else{
echo "No this object!" ";
}
}
Doing (new student ()); The students are having a class
Doing (new Office ()); The clerk is at work
This is a polymorphic feature that is flexible to reuse.
Other practices
From the way of realization of polymorphism, we can standardize the method of each class to overload the parent class, so as to achieve uniform effect. And when we define classes, it is possible to add a uniform approach to our own. So the above example can be implemented as well:
Copy the Code code as follows:
/**
* PHP polymorphism
* Jones Taiwan Blog
*/
Defining student Classes
Class student{
Define a unified approach pub
Public Function Pub () {
echo "Student work is in Class!"
";
}
}
Define the Employee class
Class office{
Define a unified approach pub
Public Function Pub () {
echo "Clerk is at work!
";
}
}
Judging Object Type methods
function doing ($obj) {
if ($obj) {
Unified method for calling classes
$obj->pub ();
}else{
Echo ' does not have this object ';
}
}
Doing (new student ()); The students are having a class
Doing (new Office ()); The clerk is at work
Of course, the above example does not show that polymorphism can be done in such a way, after all, polymorphism in complex program design, it can effectively achieve flexible reuse characteristics.
Polymorphism can also be understood as a programming approach, and the ultimate goal of programming is: flexible, polymorphic, reusable, efficient.
Object-oriented polymorphism what's going on?
Object-oriented concept, object-oriented programming, is the program designer has been pursuing, the previous programming is process-oriented, not be able to do large projects. Object-oriented programming, can be said to be a revolution. More suitable for human thought, people's thinking, not according to machine language thinking, programming! In C #, everything is an object! In fact, the object is not difficult to understand, such as: you want to find a wife, wife is a what look, how big, last name what, do what work, pay a few boyfriends, and so these are objects (wife) hold the trait.
When it comes to type conversions, this should be needless to say!
As far as the parent class object is coerced into a subclass object, the precondition is that the parent class object has the value of the subclass object, that is, the object has been assigned to the parent class variable!
///
If the parent class object is converted to a subclass object, is it called a method of the parent or subclass (or property)?
This is not good to say, to say clearly, it will take a while! The problem is more, the sub-class is not hidden, rewrite, the parent class method, besides, the parent class is abstract class, or interface, and so on, this to specific problems specific analysis!
It's best to post your code to help you analyze!
The above is my understanding, if wrong, please Haihan!
The understanding of polymorphism in object-oriented
In simple terms, polymorphism is a feature that has the ability to express many forms, in oo it means that the language has the ability to handle it in different ways depending on the type of object, especially the overloaded and inherited classes. Polymorphism is considered an essential feature of object-oriented languages.
Polymorphism has a variety of classifications, through the understanding of these categories can be more full of their understanding, here is no longer listed, please refer to the Wiki encyclopedia and Javaworld.
Polymorphism and generics (generic)
Polymorphism is actually a generic type.
Generics refer to the fact that we do not specifically encode for a particular type, but instead use a common coding method for different types, whether data results or algorithms.
Traditional generics refer to the generalization of parameters in the same way as template function, typical applications are C + + STL, such as list, vector, and algorithm.
OO has been able to do generics in the real sense through interfaces (Interface) and abstract classes. In my opinion, this is the most exciting place of Oo, that is, the power of polymorphism. And for the traditional sense of generic, I always feel that its role has been aguan.
Polymorphism and Inheritance (inheritance)
Strictly speaking, polymorphism and inheritance, overloading is not isolated, there is a close relationship between them, polymorphism is based on the two (in fact, inheritance is useful overloaded this feature).
The traditional polymorphism is actually implemented by virtual functions (virtual function) using virtual table (the most used in the early C simulation of OO features, and the implementation of C + +, the later technology has not been studied, the use of VT is unknown), and is naturally inseparable from inheritance, In other words polymorphism actually overrides inheritance.
It is because of the close relationship between inheritance and polymorphism that makes it easy for us to pigtailed.
For a common 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;
}
}
Then this kind of relationship is called inheritance, and the following usage is inherited:
Triangel tri = new Triangle ();
println ("Triangle is a type of sharp?" + tri.issharp ());
And this way is polymorphic:
Sharp sharp = new Rectangle ();
println ("My sharp have" + sharp.getsides () + "sides.");
What's the difference between the two? Obviously, inheritance is a way for subclasses to use the parent class, and polymorphism is the way that the parent class uses subclasses.
The technical difference is the binding period, late binding must be polymorphic. ... Remaining full text >>
http://www.bkjia.com/PHPjc/866666.html www.bkjia.com true http://www.bkjia.com/PHPjc/866666.html techarticle Examples of PHP object-oriented polymorphism, examples of object-oriented what is polymorphism? Polymorphism is the third feature of object-oriented language following the abstraction and inheritance of database. Polymorphic is more ...