在不创建子类型关系的情况下创建继承的任何明智的例子吗? [英] Any sensible examples of creating inheritance without creating subtyping relations?

查看:52
本文介绍了在不创建子类型关系的情况下创建继承的任何明智的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在教OOP,并试图向学生传达继承与创建两种类型之间的子类型关系之间的重要区别.例如,在C ++中,我可以使用私有继承来确保外部没有人看到子类型关系.

I've been teaching OOP and was trying to convey to my students the important difference between inheritance and the creation of a subtype relation between two types. For example, in C++, I could use private inheritance to ensure that nobody outside sees the subtyping relation.

但是,尽管我可以想到很多我不想创建子类型关系的情况(例如,通过双向链接的列表类实现Stack),但我真的没有想到好的设计示例在这里我实际上会选择遵循继承而不创建公共子绑定关系(而不是使用聚合).

However, while I can think of a lot of situations where I wouldn't want to create a subtyping relation (e.g., implementing a Stack via a doubly-linked list class), I can't really think of good design examples where I would actually choose to follow inheritance without creating a public subtying relation (rather than, say, use aggregation).

有什么好的例子的想法吗?

Any ideas of good examples?

推荐答案

作为Python程序员,我要说的是,这些问题太过微妙了.它们似乎纯粹是C ++主义.

As a Python programmer, let me say that these are matters that are way, way too subtle. They appear to be purely C++-isms.

在Python中,我们具有继承关系,该继承关系创建适当的子类型.

In Python, we have inheritance, which creates proper subtypes.

我们使用简单的属性或可用的集合进行构图.

And we have composition using simple attributes, or of the available collections.

这种私有继承业务是一种组合,似乎简直令人困惑.而且基本上没有用.

This business of private inheritance being a kind of composition seems to be simply confusing. And largely useless.

C ++示例显示了私有继承,以使用公共接口创建合成.在Java中,我们将有一个通用接口.在Python中,我们使用鸭子类型,不需要形式主义.

The C++ examples show private inheritance to create composition with a common interface. In Java, we'd have a common interface. In Python, we have duck typing and don't need the formalisms.

我认为没有很好的例子-我认为这个概念太微妙了,没有任何实际价值.

I don't think there are good examples -- I think the concept is too subtle to be of any real value.

这篇关于在不创建子类型关系的情况下创建继承的任何明智的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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