月亮星座是什么意思| 拆穿是什么意思| videos是什么意思| 宝宝益生菌什么时候吃最好| 唐朝为什么灭亡| 榨精是什么意思| 连翘败毒丸的功效与作用是什么| 立是什么生肖| 什么是妇科病| uv是什么| 经常放屁什么原因| 六角恐龙吃什么| 无孔不入是什么意思| 什么是传染性软疣| 尿路感染是什么原因| 吃什么生血快| 什么的嗓门| 输卵管堵塞吃什么药可以疏通| 食禄痣是什么意思| 医院康复科是干什么的| 漂流需要准备什么东西| 肠炎挂什么科| 什么是辐照食品| 发烧酒精擦什么部位| 禀赋是什么意思| 什么是腺样体面容| 娘家人是什么意思| 早期教育是什么专业| 中耳炎去药店买什么药| 肌酐高不能吃什么| 眼睛吹风就流泪是什么原因| 面基是什么意思| 肝肾亏虚吃什么中成药| dha中文叫什么| 锄禾是什么意思| 愚人节是什么意思| 成都人民公园有什么好玩的| 皮下出血点是什么原因| 婴儿放屁多是什么原因| 脚背浮肿是什么原因引起的| 马来西亚主食吃什么| 鱼龙混杂什么意思| 屁股痛是什么引起的| 前列腺挂什么科| 儿童鼻炎挂什么科| 舒筋健腰丸为什么这么贵| 总胆红素偏高说明什么| 煤气是什么气体| 现在是吃什么水果的季节| 肠胃炎看什么科| 肝右叶钙化灶是什么意思| 老豆腐和嫩豆腐有什么区别| 什么辉煌四字词语| 吸渣体质是什么意思| 骑自行车有什么好处| 食指比无名指长代表什么| 头发稀少是什么原因| 内痔疮用什么药治最好效果最快| 憩是什么意思| 壮志凌云是什么生肖| 什么木做菜板最好| 吃茶叶蛋有什么好处和坏处| 劳模是什么意思| 碗莲什么时候开花| 洁面膏和洗面奶有什么区别| 会所是什么意思| kyocera是什么牌子| 滋养细胞疾病是什么病| 什么是克氏综合征| 为什么不能在一起| david是什么意思| 酉什么意思| 七月4号是什么星座| 尿常规能检查出什么| 六个口是什么字| cm是什么| 为什么大医院不用宫腔镜人流| 牙疼吃什么药| close什么意思| 便秘吃什么药最好| 增生是什么| 阴茎是什么| 伏特加是用什么酿造的| 1835年属什么生肖| 脸颊两边长痘痘是什么原因引起的| 事无巨细是什么意思| 一什么绿毯| 七个小矮人分别叫什么| 过敏应该挂什么科| ctp是什么| 生物技术专业学什么| 肝硬化是什么病| 吃什么保养子宫和卵巢| 双侧卵巢多囊性改变是什么意思| 足底筋膜炎挂什么科| 局限性是什么意思| 9月16号是什么星座| 左手小指和无名指发麻是什么原因| 吃什么除体内湿气最快| 水钻是什么材质| 脱疽是什么意思| 万岁是什么意思| 24岁属什么| 抢救失血伤员时要先采取什么措施| 肝气不舒吃什么中成药| 孩子手抖是什么原因| 结节性红斑是什么原因引起的| 读警校需要什么条件| 乙肝通过什么传播| 庞统为什么要献连环计| 散光是什么原因造成的| 不感冒是什么意思| 男宝胶囊为什么不建议吃| 上山下水什么字| 什么是热病| hr是什么单位| 感康是什么| 汗疱疹涂什么药| 浅表性胃炎吃什么药效果好| 肾上腺素是什么意思| 什么叫轻断食| 血虚肝旺有什么症状有哪些| 失眠吃什么水果| 肩周炎吃什么药效果最好| 手抽筋是什么原因| 什么应外合| 梦见长大水是什么意思| 左眼皮一直跳是什么意思| 水蛭是什么动物| 人鱼小姐大结局是什么| 吃什么能提升血小板| 中风是什么| 南屏晚钟什么意思| 吃东西恶心想吐是什么原因| 为什么心率过快| more是什么意思| 心肌损伤是什么意思| 造势是什么意思| fla是什么牌子| 男人吃生蚝补什么| 黑米和什么一起搭配煮粥最佳| 全组副鼻窦炎什么意思| 菊花像什么| 身心俱疲是什么意思| 胃窦是什么意思| 复诊是什么意思| 风湿是什么| 胆汁有什么作用| 吃避孕药有什么副作用| 牙套脸是什么样| 放任是什么意思| 赶的偏旁是什么| 羞明畏光是什么意思| 11月2号是什么星座| 天启是什么意思| 4月9日什么星座| 人生没有什么不可放下| butterfly什么意思| 男人右眼跳是什么预兆| 卡鱼刺挂什么科| 膝盖骨质增生用什么药效果好| 单独是什么意思| 化验血能查出什么项目| 治疗白斑最有效的方法是什么| 肾挂什么科室| 喉咙痒咳嗽有痰是什么原因| 脑白质病变是什么病| 巴利属于什么档次的| 用什么药| 吃什么补白细胞| 红粉是什么意思| 心律不齐吃什么药最快| 外科检查一般检查什么| 部级是什么级别| 脸发红发痒是什么原因| 荨麻疹忌口什么食物| nf是什么意思| 刚出生的小猫吃什么| 什么是腹式呼吸的正确方法| 三岁宝宝喝什么奶粉比较好| 整装待发是什么意思| 肌肉抖动是什么原因| 骨质欠规整是什么意思| 这个季节吃什么菜好| 内向是什么意思| 流黄鼻涕吃什么药| 仿制药是什么意思| 螺旋藻是什么东西| 三教九流代表什么生肖| 梦见自己出轨是什么意思| 忘忧草是什么意思| 脂肪肝什么东西不能吃| 什么日| 新生儿呛奶是什么原因引起的| 怀孕初期分泌物是什么样的| 澳门打车用什么软件| 三点水的字和什么有关| 喉咙痛有黄痰吃什么药| 舌头发麻是什么原因| 中暑是什么症状表现| 什么是脚气| cot什么意思| 林彪为什么出逃| girl什么意思| 疖子是什么原因引起的| ch2o是什么物质| 什么叫淋巴结转移| 天意不可违是什么意思| 妈妈是什么| 什么原因导致子宫内膜息肉| 分辨率dpi是什么意思| 无痛人流后吃什么对身体恢复比较好| 什么力气| rn是什么意思| 为什么吃肉多反而瘦了| 静脉曲张溃烂擦什么药| 鸡是什么类| 水杯什么材质好| 菱形脸适合什么发型| 孕激素六项检查什么时候做| 吃什么对血液好| 什么叫遗精| 从未是什么意思| 垂体瘤是什么病| 梦见别人开车翻车是什么预兆| 辽宁舰舰长是什么军衔| 狼的天敌是什么动物| 肾气不足有什么症状| 什么争鸣成语| 什么时候排卵期| 都有什么瓜| vr间隙是什么意思| 暗物质是什么| 容字五行属什么| 美国报警电话为什么是911| 女贞子是什么| 踩指压板有什么好处| 血氧仪是干什么用的| 俄狄浦斯情结是什么意思| 每天早上起来口苦是什么原因| 肝疼吃什么药好| 胆囊萎缩是什么原因| 脑鸣是什么原因引起的| 4月1日什么星座| 托腮是什么意思| 利好是什么意思| 壬字五行属什么| 孕妇过敏性鼻炎可以用什么药| 千叶豆腐是什么做的| 硬性要求是什么意思| 深圳到香港需要办理什么手续| 近视散光是什么意思| 阿碧的居所叫什么名字| 晨尿有泡沫是什么原因| 周易是什么| 糖尿病不能吃什么水果| 牙龈萎缩用什么药| 方便是什么意思| 脚底板疼痛是什么原因| 肌肉抖动是什么原因| 宇宙的中心是什么| 音高是什么意思| t1什么意思| loho眼镜属于什么档次| 百度Jump to content

海南三院抢救三例河豚中毒者 提醒:切勿食用

From Wikipedia, the free encyclopedia
百度 父母目不识丁,家中连笔墨都没有,他们不希望孩子做个睁眼瞎,李可染7岁被送到私塾读书,13岁时学画山水。

In programming language theory and type theory, polymorphism is the approach that allows a value type to assume different types.[1]

In object-oriented programming, polymorphism is the provision of one interface to entities of different data types.[2] The concept is borrowed from a principle in biology where an organism or species can have many different forms or stages.[3]

The most commonly recognized major forms of polymorphism are:

  • Ad hoc polymorphism: defines a common interface for an arbitrary set of individually specified types.
  • Parametric polymorphism: not specifying concrete types and instead use abstract symbols that can substitute for any type.
  • Subtyping (also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.[4]

History

[edit]

Interest in polymorphic type systems developed significantly in the 1990s, with practical implementations beginning to appear by the end of the decade. Ad hoc polymorphism and parametric polymorphism were originally described in Christopher Strachey's Fundamental Concepts in Programming Languages,[5] where they are listed as "the two main classes" of polymorphism. Ad hoc polymorphism was a feature of ALGOL 68, while parametric polymorphism was the core feature of ML's type system.

In a 1985 paper, Peter Wegner and Luca Cardelli introduced the term inclusion polymorphism to model subtypes and inheritance,[1] citing Simula as the first programming language to implement it.

Forms

[edit]

Ad hoc polymorphism

[edit]

Christopher Strachey chose the term ad hoc polymorphism to refer to polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading).[5] The term "ad hoc" in this context is not pejorative: instead, it means that this form of polymorphism is not a fundamental feature of the type system. In the Java example below, the add functions seem to work generically over two types (integer and string) when looking at the invocations, but are considered to be two entirely distinct functions by the compiler for all intents and purposes:

class AdHocPolymorphic {
    public String add(int x, int y) {
        return "Sum: " + (x + y);
    }

    public String add(String name) {
        return "Added " + name;
    }
}

public class Adhoc {
    public static void main(String[] args) {
        AdHocPolymorphic poly = new AdHocPolymorphic();

        System.out.println(poly.add(1,2));   // prints "Sum: 3"
        System.out.println(poly.add("Jay")); // prints "Added Jay"
    }
}

In dynamically typed languages the situation can be more complex as the correct function that needs to be invoked might only be determinable at run time.

Implicit type conversion has also been defined as a form of polymorphism, referred to as "coercion polymorphism".[1][6]

Parametric polymorphism

[edit]

Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values uniformly without depending on their type.[7] Parametric polymorphism is a way to make a language more expressive while still maintaining full static type safety.

The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function. A data type that can appear to be of a generalized type (e.g., a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made.

Parametric polymorphism is ubiquitous in functional programming, where it is often simply referred to as "polymorphism". The next example in Haskell shows a parameterized list data type and two parametrically polymorphic functions on them:

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)

Parametric polymorphism is also available in several object-oriented languages. For instance, templates in C++ and D, or under the name generics in C#, Delphi, Java, and Go:

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 (and later Jean-Yves Girard) formally developed this notion of polymorphism as an extension to lambda calculus (called the polymorphic lambda calculus or System F). Any parametrically polymorphic function is necessarily restricted in what it can do, working on the shape of the data instead of its value, leading to the concept of parametricity.

Subtyping

[edit]

Some languages employ the idea of subtyping (also called subtype polymorphism or inclusion polymorphism) to restrict the range of types that can be used in a particular case of polymorphism. In these languages, subtyping 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 (according to the Liskov substitution principle). This type relation is sometimes written S <: T. Conversely, T is said to be a supertype of S, written T :> S. Subtype polymorphism is usually resolved dynamically (see below).

In the following Java example cats and dogs are made subtypes of pets. The procedure letsHear() accepts a pet, but will also work correctly if a subtype is passed to it:

abstract class Pet {
    abstract String speak();
}

class Cat extends Pet {
    String speak() {
        return "Meow!";
    }
}

class Dog extends Pet {
    String speak() {
        return "Woof!";
    }
}

static void letsHear(final Pet pet) {
    System.out.println(pet.speak());
}

static void main(String[] args) {
    letsHear(new Cat());
    letsHear(new Dog());
}

In another example, if Number, Rational, and Integer are types such that Number :> Rational and Number :> Integer (Rational and Integer as subtypes of a type Number that is a supertype of them), a function written to take a Number will work equally well when passed an Integer or Rational as when passed a Number. The actual type of the object can be hidden from clients into a black box, and accessed via object identity. If the Number type is abstract, it may not even be possible to get your hands on an object whose most-derived type is Number (see abstract data type, abstract class). This particular kind of type hierarchy is known, especially in the context of the Scheme language, as a numerical tower, and usually contains many more types.

Object-oriented programming languages offer subtype polymorphism using subclassing (also known as inheritance). In typical implementations, each class contains what is called a virtual table (shortly called vtable) — a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the vtable of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of:

  • late binding, because virtual function calls are not bound until the time of invocation;
  • single dispatch (i.e., single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (the this object), so the runtime types of the other arguments are completely irrelevant.

The same goes for most other popular object systems. Some, however, such as Common Lisp Object System, provide multiple dispatch, under which method calls are polymorphic in all arguments.

The interaction between parametric polymorphism and subtyping leads to the concepts of variance and bounded quantification.

Row polymorphism

[edit]

Row polymorphism[8] is a similar, but distinct concept from subtyping. It deals with structural types. It allows the usage of all values whose types have certain properties, without losing the remaining type information.

Polytypism

[edit]

A related concept is polytypism (or data type genericity). A polytypic function is more general than polymorphic, and in such a function, "though one can provide fixed ad hoc cases for specific data types, an ad hoc combinator is absent".[9]

Rank polymorphism

[edit]

Rank polymorphism is one of the defining features of the array programming languages, like APL. The essence of the rank-polymorphic programming model is implicitly treating all operations as aggregate operations, usable on arrays with arbitrarily many dimensions,[10] which is to say that rank polymorphism allows functions to be defined to operate on arrays of any shape and size.

Implementation aspects

[edit]

Static and dynamic polymorphism

[edit]

Polymorphism can be distinguished by when the implementation is selected: statically (at compile time) or dynamically (at run time, typically via a virtual function). This is known respectively as static dispatch and dynamic dispatch, and the corresponding forms of polymorphism are accordingly called static polymorphism and dynamic polymorphism.

Static polymorphism executes faster, because there is no dynamic dispatch overhead, but requires additional compiler support. Further, static polymorphism allows greater static analysis by compilers (notably for optimization), source code analysis tools, and human readers (programmers). Dynamic polymorphism is more flexible but slower—for example, dynamic polymorphism allows duck typing, and a dynamically linked library may operate on objects without knowing their full type.

Static polymorphism typically occurs in ad hoc polymorphism and parametric polymorphism, whereas dynamic polymorphism is usual for subtype polymorphism. However, it is possible to achieve static polymorphism with subtyping through more sophisticated use of template metaprogramming, namely the curiously recurring template pattern.

When polymorphism is exposed via a library, static polymorphism becomes impossible for dynamic libraries as there is no way of knowing what types the parameters are when the shared object is built. While languages like C++ and Rust use monomorphized templates, the Swift programming language makes extensive use of dynamic dispatch to build the application binary interface for these libraries by default. As a result, more code can be shared for a reduced system size at the cost of runtime overhead.[11]

See also

[edit]

References

[edit]
  1. ^ a b c Cardelli, Luca; Wegner, Peter (December 1985). "On understanding types, data abstraction, and polymorphism" (PDF). ACM Computing Surveys. 17 (4): 471–523. CiteSeerX 10.1.1.117.695. doi:10.1145/6041.6042. S2CID 2921816.: "Polymorphic types are types whose operations are applicable to values of more than one type."
  2. ^ Stroustrup, Bjarne (February 19, 2007). "Bjarne Stroustrup's C++ Glossary". polymorphism – providing a single interface to entities of different types.
  3. ^ "Polymorphism". The Java Tutorials: Learning the Java Language: Interfaces and Inheritance. Oracle. Retrieved 2025-08-06.
  4. ^ Conallen, J.; Engle, M.; Houston, K.; Maksimchuk, R.; Young, B.; Booch, G. (2007). Object-Oriented Analysis and Design with Applications (3rd ed.). Pearson Education. ISBN 9780132797443.
  5. ^ a b Strachey, Christopher (2000). "Fundamental Concepts in Programming Languages". Higher-Order and Symbolic Computation. 13 (1/2): 11–49. CiteSeerX 10.1.1.332.3161. doi:10.1023/A:1010000313106. ISSN 1573-0557. S2CID 14124601.
  6. ^ Tucker, Allen B. (2004). Computer Science Handbook (2nd ed.). Taylor & Francis. pp. 91–. ISBN 978-1-58488-360-9.
  7. ^ Pierce, B.C. (2002). "23.2 Varieties of Polymorphism". Types and Programming Languages. MIT Press. pp. 340–1. ISBN 9780262162098.
  8. ^ Wand, Mitchell (June 1989). "Type inference for record concatenation and multiple inheritance". Proceedings. Fourth Annual Symposium on Logic in Computer Science. pp. 92–97. doi:10.1109/LICS.1989.39162.
  9. ^ L?mmel, Ralf; Visser, Joost (2002). "Typed Combinators for Generic Traversal". Practical Aspects of Declarative Languages: 4th International Symposium. Springer. pp. 137–154, See p. 153. CiteSeerX 10.1.1.18.5727. ISBN 354043092X.
  10. ^ Slepak, Justin; Shivers, Olin; Manolios, Panagiotis (2019). "The semantics of rank polymorphism". arXiv:1907.00509 [cs.PL].
  11. ^ Beingessner, Alexis. "How Swift Achieved Dynamic Linking Where Rust Couldn't".
[edit]
脚后跟疼什么原因 什么样的荷花 心电图逆钟向转位什么意思 补肾气吃什么药 黑色素沉淀是什么原因引起的
2月23号是什么星座 男人说冷静一段时间是什么意思 伪娘什么意思 俄狄浦斯情结是什么意思 什么是阴历什么是阳历
格拉苏蒂手表什么档次 新生儿痤疮用什么药膏 此言差矣是什么意思 7月15号是什么星座 葫芦什么时候开花
大便干燥一粒一粒的吃什么药 气血虚吃什么好 三点水加个有字念什么 中药学是什么 空腹喝可乐有什么危害
血虚吃什么中成药hcv8jop2ns5r.cn 小米粥和什么搭配最好最养胃hcv9jop5ns9r.cn 颈椎病用什么枕头好hcv8jop7ns2r.cn 吃生蚝有什么好处hcv8jop0ns8r.cn 乳酸菌可以制作什么hcv9jop6ns9r.cn
欺世盗名是什么意思hcv7jop6ns3r.cn 黎字五行属什么hcv9jop7ns1r.cn 但爱鲈鱼美的但是什么意思creativexi.com 依达拉奉注射功效与作用是什么hcv9jop2ns3r.cn 空降是什么意思xinjiangjialails.com
小麦是什么粮食hcv8jop3ns8r.cn 月色真美什么意思shenchushe.com 小孩阑尾炎是由什么原因引起的hcv8jop6ns7r.cn 姑姑家的儿子叫什么hcv9jop5ns7r.cn 6月7日什么星座hcv8jop6ns1r.cn
喝什么酒不会胖hcv9jop2ns6r.cn 什么叫切片hcv8jop7ns2r.cn 坐飞机不能带什么物品hcv9jop3ns8r.cn 忐忑不安是什么意思hcv7jop5ns5r.cn 吃什么能补肾hcv8jop1ns7r.cn
百度