What is polymorphism, what is it for, and how is it used?

前端 未结 28 2723
南笙
南笙 2020-11-21 07:08

What is polymorphism, what is it for, and how is it used?

28条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 07:57

    Polymorphism allows the same routine (function, method) to act on different types.

    Since many existing answers are conflating subtyping with polymorphism, here are three ways (including subtyping) to implement polymorphism.

    • Parameteric (generic) polymorphism allows a routine to accept one or more type parameters, in addition to normal parameters, and runs itself on those types.
    • Subtype polymorphism allows a routine to act on any subtype of its parameters.
    • Ad hoc polymorphism generally uses routine overloading to grant polymorphic behavior, but can refer to other polymorphism implementations too.

    See also:

    http://wiki.c2.com/?CategoryPolymorphism

    https://en.wikipedia.org/wiki/Polymorphism_(computer_science)

提交回复
热议问题