多态性与继承有关 [英] polymorphism relates inheritance
问题描述
为什么多态性依赖于继承?代码示例?
why polymorphism is dependent on inheritance? code example?
推荐答案
多态性不天生就依赖于继承.
Polymorphism is not inherently dependent on inheritance.
多态是一个相当抽象的概念,它为不同类型的值提供统一的接口.
Polymorphism is a rather abstract concept about giving uniformous interfaces to different kinds of values.
在 Java 或 C# 等常见的面向对象语言中,这些接口是通过类继承提供的,但这是多态性的一种可能实现,而不是一般的多态性概念.
In common object-oriented languages like Java or C#, these interfaces are provided through class inheritance, but this is one possible implementation of polymorphism and not the concept of polymorphism in general.
Duck 类型、结构类型、C++ 样式模板或类型类都提供了多态性的其他实现.
Duck typing, structural typing, C++-style templates or typeclasses all provide other implementations of polymorphism.
只需查看所有这些多态代码,以便为鸭子提供接口......
Just see all this polymorphous codes in order to provide an interface to ducks ...
interface Duck { void quak(); }
void DoQuak(Duck d) { d.quak(); }
def DoQuak(d):
d.quak();
template <typename T>
void DoQuak(const T& d) { d.quak(); }
def DoQuak(d : { def quak() : Unit }) { d.quak() }
class Quakable a where
quak :: a -> IO ()
doQuak d = quak d
这篇关于多态性与继承有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!