面向对象:多态 Polymorphism、重载、重写、继承
2017-10-07 16:31
246 查看
1. 声明
本文来源于维基百科:Polymorphism (computer science)
2.定义
In programming languages and type theory, polymorphism or polyphormism is the provision of a single interface to entities of different types.对不同类型实体的单一接口
3.分类
注:分类方法之一3.1 Ad hoc polymorphism
when a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations. Ad hoc polymorphism is supported in many languages using function overloadin代码示例:
program Adhoc; function Add(x, y : Integer) : Integer; begin Add := x + y end; function Add(s, t : String) : String; begin Add := Concat(s, t) end; begin Writeln(Add(1, 2)); (* Prints "3" *) Writeln(Add('Hello, ', 'World!')); (* Prints "Hello, World!" *) end.
Ad-hoc polymorphism 又称重载 Overloading( function overloading or operator overloading)
3.2 Parametric polymorphism
when 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, this is often known as generics or generic programming. In the functional programming community, this is often shortened to polymorphism.代码示例:
class List<T> { class Node<T> { T elem; Node<T> next; } Node<T> head; int length() { ... } } List<B> map(Func<A, B> f, List<A> xs) { ... }
Parametric polymorphism
1. 在C++中体现为 templates 模板
2. 在Java中体现为 generics 泛型
3.3 Subtyping
(also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass. In the object-oriented programming community, this is often referred to as simply Inheritance.代码示例:
abstract class Animal { abstract String talk(); } class Cat extends Animal { String talk() { return "Meow!"; } } class Dog extends Animal { String talk() { return "Woof!"; } } static void letsHear(final Animal a) { println(a.talk()); } static void main(String[] args) { letsHear(new Cat()); letsHear(new Dog()); }
Subtype polymorphism 体现为 继承 Inheritance 后的 重写 Overriding
相关文章推荐
- java 继承 多态(override->重写(=覆盖)、overload->重载、polymorphism -> 多态)
- php部分--面向对象三大特性-封装(另加连续调用的一个例子)、继承(重写、重载的例子)、多态;
- php 面向对象的三大要素(封装、继承、多态)以及重写(override)和重载(overload)的举例说明
- Day2 面向对象(封装·继承·多态·重载·重写)
- 对象之间的通信,面向对象的三大特征,封装,继承,多态;以及重写,重载
- 封装,继承,多态,重载,重写和泛型详解(附图)-
- JAVA面试题解惑系列(九)——继承、多态、重载和重写
- Java中继承、多态、重载和重写
- JAVA【类】析构器、重载与继承、方法的重写、自动转型、多态、方法与类的分类、接口继承
- 继承、多态、重载和重写
- JAVA——继承、多态、重载和重写---转
- 继承,多态,重载和重写的区别。
- 继承、多态、重载和重写
- 重载和重写、继承和多态的区别
- 面向对象的三大特点:封装,继承,多态,外加一个重载概念。
- JAVA——继承、多态、重载和重写
- 继承与多态 --- 下 --- 重载与重写、多态实现原理、纯虚函数
- JAVA面试题解惑系列(九)——继承、多态、重载和重写
- JAVA面试题解惑系列(九)――继承、多态、重载和重写
- JAVA面试题解惑系列(九)——继承、多态、重载和重写