为什么C#不使用多继承?(from stackoverflow)
简单地说:是因为该语言的设计者决定不使用。
基本上,.NET和Java的设计者不使用多继承(MI),是因为他们认为给语言加上多继承获得的好处较少,抵不上因此增加的复杂性。
1、不同的语言对于多继承如何工作实际上有不同的期望。比如说,在冲突时基类如何合并或处理冗余。在CLR中实现多继承之前,我们需要查看一下所有其他的语言,看看一些共通的概念,并在一种语言-中立的方式来表示它们。我们还需要决定多继承是否应存在 于CLS,而这对于不想要这个概念的语言(比如说可能有VB.NET)。当然,这本是公共语言运行时的分内之事,然而我们没有找到足够的时间为此来做。
2、多继承真正合适的地方实际上很少。在很多情况下,多接口实现都可以做这个事。在其他情况下,你可以使用封装和委托。假如我们添加一个稍微不同的构造器,如mixins,会更强大吗?
3、多继承给实现增加了很多的复杂性。这个复杂性影响了类型转换,布局,字段访问,序列化,身份比较,可验证,反射,泛型以及可能还有很多别的地方。
原文:
The short answer is: because the language designers decided not to.
Basically, it seemed that both the .NET and Java designers did not allow multiple inheritance because they reasoned that adding MI added too much complexity to the languages while providing too little benefit.
Different languages actually have different expectations for how MI works. For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.
The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?
Multiple implementation inheritance injects a lot of complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.
引自:http://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c
为什么C#不使用多继承?(from stackoverflow)的更多相关文章
- C++ 系列:继承
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- Stackoverflow/dapper的Dapper-Extensions用法(二)
之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...
- Stackoverflow/dapper的Dapper-Extensions用法(一)
Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...
- 深入浅出JS的封装与继承
JS虽然是一个面向对象的语言,但是不是典型的面向对象语言.Java/C++的面向对象是object - class的关系,而JS是object - object的关系,中间通过原型prototype连 ...
- Java中的继承与组合(转载)
本文主要说明Java中继承与组合的概念,以及它们之间的联系与区别.首先文章会给出一小段代码示例,用于展示到底什么是继承.然后演示如何通过“组合”来改进这种继承的设计机制.最后总结这两者的应用场景,即到 ...
- 关于opacity透明度子元素继承现象的若干研究以及hack方法
[感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...
- Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
在C++中,类与类之间的关系大概有四种,分别为继承,关联,聚合,和组合.其中继承我们大家应该都比较熟悉,因为是C++的三大特性继承Inheritance,封装Encapsulation,和多态Poly ...
- StackOverflow:7个你从未见过的Java问题最佳答案
本文由码农网 – 李俊英原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 对开发人员来说, StackOverflow就像一个金矿.对具体的问题,它能帮我们找到最有用的答案,并且我们也 ...
- 继承BaseAdapter实现Filterable的adapter类完整示例
转载:http://www.lai18.com/content/1631130.html 目标:自定义ListView项布局通常需要自己实现Adapter,并通过搜索关键字筛选部分数据.且关键字变长变 ...
随机推荐
- wp8 入门到精通 聊天控件
<Grid > <Grid x:Name="bubble_right" VerticalAlignment="Center" RenderTr ...
- 【使用Unity开发Windows Phone上的2D游戏】(1)千里之行始于足下
写在前面的 其实这个名字起得不太欠当,Unity本身是很强大的工具,可以部署到很多个平台,而不仅仅是可以开发Windows Phone上的游戏. 只不过本人是Windows Phone 应用开发出身, ...
- 面向服务的架构SOA
SOA简介 SCA实现SOA的最佳方式 Apache开源框架Tuscany实现SCA架构 SOA简单描述: SOA(Service-Oriented Architecture)面向服务的体系架构.为了 ...
- 通信原理实践(四)——模拟通信系统性能分析
一.模拟通信系统性能分析 1.系统框图 2.信噪比定义 (1)输入信噪比: (2)输出信噪比: (3)调制制度增益: 3.模拟通信系统分析等价模型 即自己产生一个高斯白噪声,加入到调制信号,然后在送入 ...
- CF735C 数论\平衡树叶子节点的最大深度\贪心\斐波那契\条件归一化
http://codeforces.com/problemset/problem/735/C 题意..采用淘汰赛制..只要打输就退出比赛..而且只有两个选手打过的场数 相差不超过1才能比赛..最后问你 ...
- HDU 4496 D-City (并查集)
题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数 题解:正难则反,我们反向考虑使用并查集添边.首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节 ...
- ubuntu安装shadowshocks-qt5
sudo add-apt-repository ppa:hzwhuang/ss-qt5 sudo apt-get update sudo apt-get install shadowsocks-qt5 ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
- react-基础(1)
props 创建组件 React.createClass; 直接继承React.Component;与上面不同的是初始化props和state的方法: export class Counter ext ...
- 原生 js 写分页
欢迎留言或者加本人QQ172360937咨询 这段代码是用原生 js 写的一个分页的效果 <!doctype html> <html lang="en"> ...