身份证拍照穿什么衣服| 吃什么食物治便秘| 喝红酒对身体有什么好处| 辛辣是什么意思| 梦见掉牙齿是什么意思| 粘人是什么意思| 笑点低的人说明什么| 3月26日是什么节日| 艾滋病潜伏期有什么症状| 今天开什么奖| 预防更年期提前应该吃点什么药| 梵蒂冈为什么没人敢打| 器皿是什么意思| 脚面肿是什么原因| 吃什么不长肉还能瘦| 男人阴虱用什么药| 血热是什么意思| 男人阳气不足有什么症状| 毅力是什么意思| 褪黑素有什么作用| 新生儿血糖低是什么原因| 头皮发白是什么原因| 男人为什么会出轨| 好马不吃回头草什么意思| 头热是什么原因| 婴儿口臭是什么原因引起的| 严惩不贷是什么意思| 裂帛是什么意思| 家里进黄鼠狼是什么预兆| 庶子是什么意思| 什么样的人爱长结节| 万箭穿心代表什么生肖| 口字五行属什么| 78年属什么| 幽门螺旋杆菌弱阳性是什么意思| 西洋参可以和什么一起泡水喝| 左侧头皮发麻是什么原因| 吃完饭打嗝是什么原因| 孕妇可以吃什么| edenbo是什么牌子| 羊肉配什么菜好吃| 马头岩肉桂是什么茶| e代表什么数字| 维生素c补什么| 看心理医生挂什么科| 坐小月子可以吃什么水果| 手电筒的金属外壳相当于电路中的什么| 苏轼是什么居士| 智齿什么时候长| 新生儿晚上哭闹不睡觉是什么原因| 阑尾炎输液输什么药| 梦里见血代表什么预兆| Ca是什么| 看花灯是什么节日| 宫颈lsil是什么意思| 胎盘吃了对身体有什么好处| 新百伦鞋子什么档次| 什么的琴声| 痛风性关节炎吃什么药| 排骨汤用什么排骨| 牙疼吃什么药消炎最快| 狗狗体内驱虫用什么药最好| 塔罗是什么| 牛黄是什么东西| 什么叫低级别上皮内瘤变| 酸菜鱼是用什么鱼| 什么是公历| 金黄金黄的什么| 孕妇可以喝什么茶| 什么叫无产阶级| hpv会有什么症状| 矫正视力是什么意思| 南屏晚钟什么意思| 血块多是什么原因| 老戏骨是什么意思| 肾结石要忌口什么东西| 脾肾两虚吃什么中成药| 左手臂发麻是什么原因| 碳酸钙d3片什么时候吃最好| 因人而异是什么意思| 企业hr是什么意思| 房间放什么可以驱蜈蚣| 人中上窄下宽代表什么| 肝有问题会出现什么症状| 男士补肾吃什么| 黑豆熟地水功效是什么| 例假血发黑是什么原因| 六畜兴旺是什么意思| 毛主席什么时候死的| 情绪化什么意思| 脸肿脚肿是什么原因引起的| 请示是什么意思| 辐照食品是什么意思| 共建是什么意思| 唇色深的人适合什么颜色的口红| 七十岁老人装什么牙合适| 洗劫一空是什么意思| 痔疮疼痛用什么药| 小孩表演后卸妆用什么| 2016年属猴是什么命| 娥皇女英是什么意思| 湿气是什么意思| 梦见自己拉粑粑是什么意思| 肺结节是什么症状| 大圈什么意思| 内痔有什么症状| 支付宝余额和余额宝有什么区别| bell什么意思| 臭虫长什么样子图片| 冷感冒吃什么药| 人肉是什么味道的| 看病人送什么| 平板支撑练什么| 常喝红茶有什么好处| 德国是什么人种| 气虚血虚吃什么补最快| 有胃病的人吃什么最养胃| 湿疹吃什么食物好| 黄皮适合什么颜色的衣服| 干咳嗽无痰是什么原因| 双侧胸膜局限性增厚是什么意思| 小孩心跳快是什么原因| 压力大会有什么症状| 吃茄子有什么好处和坏处| 喝茶叶水有什么好处和坏处| 祥五行属什么| 肾功能检查挂什么科| 心脏缺血吃什么补的快| 未时属什么生肖| 男人早泄吃什么药| 什么溪流| 丝瓜有什么好处| 气血不足有什么症状| 怀孕的人梦见蛇是什么意思| 冬瓜有什么功效和作用| 为什么不建议做融合手术| 闭关什么意思| 五朵玫瑰花代表什么意思| 甲醛什么味| 心气不足是什么意思| 肺癌早期有什么症状| 吃什么东西补血最快最有效| 拔牙后能吃什么| 小孩肠胃炎吃什么药| 瘦马是什么意思| 宝宝多吃什么蔬菜好| 瑶五行属性是什么| 心字旁的字有什么| 吃什么容易消化| 食少便溏是什么意思| 忌神是什么意思| 什么也不懂| 眼睛模糊流泪用什么药| 低压高吃什么药| crl是什么意思| 列文虎克发现了什么| 经期是什么意思| 长脓包是什么原因| 龙阳之好是什么意思| 祛斑喝什么花茶最有效| 下腹坠胀是什么原因| 救济的近义词是什么| 芊芊是什么颜色| 为什么有的人特别招蚊子| 外耳炎用什么药| lee是什么品牌| 吃灵芝孢子粉有什么好处| 距离产生美是什么意思| 烈士家属有什么待遇| 怀孕脉象是什么样子| 床上有横梁有什么害处| 贝兄念什么| 焗油和染发有什么区别| 什么叫幽门螺旋杆菌| 脑梗可以吃什么水果| 脑血栓是什么原因引起的| 爱字五行属什么| lg什么意思| 尼泊尔人是什么人种| 顾家什么意思| 红细胞低吃什么补得快| 为什么会得带状疱疹| 胎动少是什么原因| 爷俩是什么意思| 坐飞机需要什么证件| 梦见自己大肚子怀孕是什么意思| 肾虚型脱发是什么样子| 湿化瓶内放什么水| 给孩子测骨龄应该挂什么科| 胃炎伴糜烂吃什么药效果好| 人工荨麻疹是什么原因引起的| 怕冷又怕热是什么原因| 小娘皮什么意思| gs是什么| 一什么无什么| sey什么意思| 肠胃属于什么科| 农历八月初五是什么星座| 什么是皈依| 鳄龟吃什么食物| 锁阳是什么东西| 天庭是什么意思| 腮腺炎吃什么药最管用| 梦见修路是什么预兆| 喉咙发苦是什么原因造成的| 巴基斯坦用什么语言| 03年属什么的| 蝾螈是什么动物| 沁是什么意思| 死心眼什么意思| 咽喉炎吃什么药最好| 什么人容易得白肺病| 孕妇吃什么补钙| 什么茶能去体内湿气| 什么药清肺最好| 梦见黑山羊是什么预兆| 近水楼台先得月是什么生肖| 但愿人长久的下一句是什么| 狭隘是什么意思| 欢喜是什么意思| 游走是什么意思| 什么茶解暑| 6月8日什么星座| 六月初六是什么星座| 吃白糖有什么好处和坏处| 练八段锦有什么好处| 房颤吃什么药| 项羽是什么生肖| 孕妇的尿液有什么用途| 五行白色属什么| 房水由什么产生| 朱砂痣什么意思| 慢性子宫颈炎是什么意思| 七月份是什么季节| theme什么意思| 吃什么能让月经快点来| 2010年是什么命| 滴水不漏什么意思| 黄帝叫什么名字| 相依相偎是什么意思| 大便遇水就散什么原因| ipa是什么意思| 荔枝补什么| 阴部痒是什么原因| 血脂稠吃什么药最好| 月经血量少是什么原因| 闲云野鹤是什么意思| 祭日和忌日是什么意思| 1994年是什么命| 0到3个月的婴儿惊吓吃什么药| 时辰宜忌是什么意思| 乙肝145阳性是什么意思| 手信是什么东西| 乙肝阻断针什么时候打| 萘普生是什么药| 夏天白鸽煲什么汤最好| 吃什么对大脑记忆力好| 江与河有什么区别| 花椒桂圆艾绒敷肚脐有什么作用| 稳是什么意思| 阴道炎要用什么药| crab是什么意思| 积福是什么意思| 高的部首是什么| 百度Jump to content

麻省理工研发出电子鱼“SoFi” 帮科学家探究海底秘密

From Wikipedia, the free encyclopedia
百度 “我的一些学生也在看《武媚娘传奇》,大家会讨论演绎的历史和真实的历史有多大差距。

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-07.
  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]
舌头臭是什么原因 梦见死人复活什么预兆 红顶商人是什么意思 促排卵针什么时候打 专车是什么意思
一眼万年是什么意思 手脚麻木是什么原因 硒是什么东西 6.5号是什么星座 黄瓜长什么样
中元节又叫什么节 三线炎有什么症状 舌苔厚白中间有裂纹吃什么药 经期适合吃什么 胸部dr是什么
吃什么可以缓解焦虑 眉毛白是什么原因引起的 什么是阴茎 心里难受是什么原因 做梦梦见鬼是什么预兆
骶1隐裂是什么意思youbangsi.com 铁锈用什么能洗掉creativexi.com 花嫁是什么意思hcv7jop7ns4r.cn 晚上喝红酒有什么好处和坏处hcv9jop0ns9r.cn 重阳节送老人什么礼物hcv8jop3ns2r.cn
为什么会有肾结石dajiketang.com 质子是什么hcv8jop1ns1r.cn 药流后吃什么消炎药hcv9jop4ns8r.cn 考号是什么hcv9jop3ns9r.cn 尿常规3个加号什么意思hcv8jop7ns8r.cn
酒后大量出虚汗什么原因96micro.com 什么什么满门hcv9jop3ns6r.cn 什么时候恢复高考hcv7jop9ns7r.cn 睡眠不好总做梦是什么原因shenchushe.com 老打喷嚏是什么原因hcv9jop3ns4r.cn
工资5k是什么意思hcv8jop9ns5r.cn 日加军念什么0735v.com 大红袍属于什么档次hcv8jop9ns0r.cn 心脏t波改变吃什么药hcv9jop7ns0r.cn 什么叫次日xianpinbao.com
百度