为什么c#不允许多重继承? [英] How come c# don't allow multiple inheritance?

查看:74
本文介绍了为什么c#不允许多重继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么一个还没有10年历史的编程语言不支持

多重继承?我有两个类,两个有很多字段和

方法,我需要从我的第三课,所以我试过:


class myClass3:myClass1,myClass2 {


并得到一个错误说我不能。 C#如何与C ++竞争如果

你甚至不能做出这么简单的事情?

有没有解决方法?


Patrick


***通过开发人员指南 http: //www.developersdex.com ***

解决方案

" Patrick F" <哒** @ pafo.net>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...

为什么编程语言还不到10年就不支持
多重继承?


这个问题经常出现......
http://www.google.com/搜索?sourcei ...继承%22


参见: http://blogs.msdn.com/csharpfaq/arch.../07/85562.aspx

直接来自马的嘴巴,因为它是...

有没有解决方法?




多接口继承


我所看到的接口不能包含Fields,我使用的两个
类都包含字段。我喜欢这个(仅用于

演示):


A - 汽车类(很多数据字段)

AC - 动态的A(字段)数组,因此我可以添加和删除数组

元素(如que / stack),很多用于从A中提取数据的方法
>
B - 总线类(很多数据字段)

BC - B(字段)数组是动态的,所以我可以添加和删除数组

元素(如一个que / stack),很多用于从中提取数据的方法
B


C - Traffic class,witch需要AC和BC类,然后从C i

有方法,例如。计算A中的汽车数量,并在BC省公交车阵列的中间位置编辑一辆公共汽车。


程序D - C数组(多个交通)场景),D应该能够通过C中的所有交通等级循环
并从班级获得汽车数量

AC


如果允许多重继承,这将非常简单:

class C:AC,BC {}


ClassC [] D = new ClassC [5];


D [0] .ACountCars或D [1] .BEditBus(5).Passangers = 10


当我尝试在类BC中使用接口我得到以下错误:

错误1接口不能包含字段


接口BC无法处理B对象的数组。


你怎么解决这个问题?


Patrick


***发送来自开发人员指南 http://www.developersdex.com ***

C#suppor通过接口进行多重继承。不支持通过

班级进行多重继承。


-


________________________

热烈的问候,

Alvin Bruney [MVP ASP.NET]


[无耻作者插件]

专业版VSTO.NET - Wrox / Wiley

OWC使用.NET的黑皮书
www.lulu.com/owc ,亚马逊

博客: http://www.msmvps.com/ blogs / alvin

----------------------------------- --------------------


" Patrick F" <哒** @ pafo.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...



为什么一个还没有10年历史的编程语言不支持
多重继承?我有2个类,都有很多字段和
方法我需要从我的第三节课开始,所以我尝试了:

类myClass3:myClass1,myClass2 {

并且收到错误说我不能。 C#如何与C ++竞争如果你甚至不能做出类似的简单事情?
有没有解决方法?

Patrick

***通过开发人员指南 http://www.developersdex.com 发送***



Hi,

How come that a programming language not yet 10years old dosen''t support
Multiple inherritances?, I got 2 classes, both with alot of fields and
methods that i need from my third class, so i tried:

class myClass3 : myClass1, myClass2 {

and got an error says that i cant. How can C# be a compete with C++ if
you cant even make a simple thing like that?
Is there a work around?

Patrick

*** Sent via Developersdex http://www.developersdex.com ***

解决方案

"Patrick F" <da**@pafo.net> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

How come that a programming language not yet 10years old dosen''t support
Multiple inherritances?
This question comes up regularly...
http://www.google.com/search?sourcei...inheritance%22

See also: http://blogs.msdn.com/csharpfaq/arch.../07/85562.aspx
straight from the horse''s mouth, as it were...
Is there a work around?



Multiple Interface Inheritance


Interfaces from what i have seen cant contain Fields, and both my
classes that i am using contains fields. I have it like this (just for
demonstration):

A - Car class (alot of fields for data)
AC - Array of A (field) that is dynamic so i can add and remove Array
elements (like a que/stack), alot of methods for extracting data from A

B - Bus class (alot of fields for data)
BC - Array of B (field) that is dynamic so i can add and remove Array
elements (like a que/stack), , alot of methods for extracting data from
B

C - Traffic class, witch needs both AC and BC classes, then from C i
have methods to eg. count the number of cars in A and edit one bus in
the middle of the bus array in BC.

Program D - Array of C (multiple trafic scenarios), D should be able to
loop thru all Traffic Classes in C and get the count of Cars from Class
AC

This would be very simple to do if multiple inherritance were allowed:
class C : AC, BC {}

ClassC[] D = new ClassC[5];

D[0].ACountCars or D[1].BEditBus(5).Passangers = 10

When i try to use interface in class BC i get the following error:
Error 1 Interfaces cannot contain fields

The interface BC can not handle the Array of B objects.

How do you get around this?

Patrick

*** Sent via Developersdex http://www.developersdex.com ***


C# supports multiple inheritance thru interfaces. Multiple inheritance thru
classes is not supported.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"Patrick F" <da**@pafo.net> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

Hi,

How come that a programming language not yet 10years old dosen''t support
Multiple inherritances?, I got 2 classes, both with alot of fields and
methods that i need from my third class, so i tried:

class myClass3 : myClass1, myClass2 {

and got an error says that i cant. How can C# be a compete with C++ if
you cant even make a simple thing like that?
Is there a work around?

Patrick

*** Sent via Developersdex http://www.developersdex.com ***



这篇关于为什么c#不允许多重继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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