多态性与继承有关 [英] polymorphism relates inheritance

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

问题描述

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

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 ...

继承/接口 (C#):

interface Duck { void quak(); }

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

动态 Duck 类型(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天全站免登陆