多 态 (计算机 科学 )
历史
编辑类别
编辑特 设多态
编辑
Add
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)); (* 打 印 "3" *)
Writeln(Add('Hello, ', 'Mammals!')); (* 打 印 "Hello, Mammals!" *)
end.
隐式类型转换也被
参 数多 态
编辑
append
,它不关心a
”append
forall a. [a] × [a] -> [a]
这里[a]
a
a
append
a
append
a
确定
data List a = Nil | Cons a (List a)
length :: List a -> Integer
length Nil = 0
length (Cons x xs) = 1 + length xs
map :: (a -> b) -> List a -> List b
map f Nil = Nil
map f (Cons x xs) = Cons (f x) (map f xs)
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) {
...
}
John C. Reynolds(
子 类型
编辑
letsHear()
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());
}
实现角度 类别
编辑
- 动态
多 态(dynamic polymorphism):生 效 于运行期 。 静 态多态(static polymorphism):将 不同 的 特殊 行 为和单个泛化记号相 关联,由 于这种关联处理 于编译期 而非运行期 ,因 此被称 为“静 态”。可 以用来 实现类型安全 、运行高 效 的 同 质对象 集合 操作 。C++ STL不 采 用 动态多 态来实现就是个例子 。
对于C++语言,带变
参 见
编辑
参考 资料
编辑
- ^ Bjarne Stroustrup. Bjarne Stroustrup's C++ Glossary. February 19, 2007 [2018-06-29]. (
原始 内容 存 档于2018-06-29).polymorphism – providing a single interface to entities of different types.
- ^ 2.0 2.1 2.2 Cardelli, Luca; Wegner, Peter. On understanding types, data abstraction, and polymorphism (PDF). ACM Computing Surveys (New York, NY, USA: ACM). December 1985, 17 (4): 471–523 [2018-06-29]. ISSN 0360-0300. doi:10.1145/6041.6042. (
原始 内容 (PDF)存 档于2019-10-14).: "Polymorphic types are types whose operations are applicable to values of more than one type." - ^ Booch, et al 2007 Object-Oriented Analysis and Design with Applications. Addison-Wesley.
- ^ Strachey, Christopher. Fundamental Concepts in Programming Languages. Higher-Order and Symbolic Computation. 2000, 13 (1/2): 11–49. ISSN 1573-0557. doi:10.1023/A:1010000313106.
- ^ Milner, R., Morris, L., Newey, M. "A Logic for Computable Functions with reflexive and polymorphic types", Proc. Conference on Proving and Improving Programs, Arc-et-Senans (1975)
- ^ Christopher Strachey. Fundamental Concepts in Programming Languages (PDF). www.itu.dk (Kluwer Academic Publishers). [2012-10-13]. (
原始 内容 存 档 (PDF)于2017-08-12). - ^ Allen B. Tucker. Computer Science Handbook, Second Edition. Taylor & Francis. 28 June 2004: 91– [2021-02-06]. ISBN 978-1-58488-360-9. (
原始 内容 存 档于2017-03-31). - ^ Pierce, B. C. 2002 Types and Programming Languages. MIT Press.