多态性与遗传有关 [英] polymorphism relates inheritance

查看:70
本文介绍了多态性与遗传有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么多态性依赖于继承?代码示例?

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.

鸭子类型,结构类型,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 ...

继承/接口(C#):

interface Duck { void quak(); }

void DoQuak(Duck d) { d.quak(); }

动态鸭子输入(Python):

def DoQuak(d):
    d.quak();

模板(静态鸭式)(C ++):

template <typename T>
void DoQuak(const T& d) { d.quak(); }

结构类型(Scala):

def DoQuak(d : { def quak() : Unit }) { d.quak() }

类型类别(Haskell):

class Quakable a where
    quak :: a -> IO ()

doQuak d = quak d

这篇关于多态性与遗传有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆