http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://www.cplusplus.com/doc/tutorial/polymorphism/
http://www.codeproject.com/Articles/10900/Polymorphism-in-C
多态
多型(英语:Polymorphism),是指物件導向程式執行時,相同的訊息可能會送給多個不同的類別之物件,而系統可依據物件所屬類別,引發對應類別的方法,而有不同的行為。簡單來說,所謂多型意指相同的訊息給予不同的物件會引發不同的動作稱之。比如有動物(Animal)之類別(Class),而且由動物繼承出類別雞(Chicken)和類別狗(Dog),並對同一源自類別動物(父類別)之一訊息有不同的響應,如類別動物有「叫()」之動作,而類別雞會「啼叫()」,類別狗則會「吠叫()」,則稱之為多型。
In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.
1. ad-hoc polymorphism. Ad-hoc polymorphism is supported in many languages using function and method overloading.
2. parametric polymorphism: If all code is written without mention of any specific type and thus can be used transparently with any number of new types.In the object-oriented programming community, programming using parametric polymorphism is often called generic programming.
3. subtype polymorphism or inclusion polymorphism, a name may denote instances of many different classes as long as they are related by some common super class. objects of different types are entirely substitutable for objects of another type (their base type(s)) and thus can be handled via a common interface.
Ad-hoc polymorphism = function/operator overloading
ad-hoc polymorphism to refer to polymorphic functions which can be applied to arguments of different types, but which behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading).
Parametric polymorphism = genetics
Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values identically without depending on their type.
class List<T> {
class Node<T> {
T elem;
Node<T> next;
}
Node<T> head;
int length() { ... }
}
List map(Func<A,B> f, List<A> xs) {
...
}
subtyping polymorphism(sometimes referred to as dynamic polymorphism) allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T.
abstract class Animal {
String talk();
}
class Cat extends Animal {
String talk() { return "Meow!"; }
}
class Dog extends Animal {
String talk() { return "Woof!"; }
}
public class MyClass {
public static void write(Animal a) {
System.out.println(a.talk());
}
public static void main() {
write(new Cat());
write(new Dog());
}
}
one C++ example:
// dynamic allocation and polymorphism
#include <iostream>
using namespace std;
class CPolygon { // abstract base class
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0; // virtual members
void printarea (void)
{ cout << this->area() << endl; } // pure virtual members can be called from the abstract base class
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
CPolygon * ppoly1 = new CRectangle; // pointers to base class
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}