在C#中的多重继承 [英] Multiple Inheritance in C#

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

问题描述

由于多重继承是坏(它使源更复杂)C#不直接提供这样的模式。但有时这将是有帮助的这种能力。

比如我可以在使用接口和三个类一样,实行缺少多重继承模式:

 公共接口IFirst {无效FirstMethod(); }
公共接口ISecond {无效SecondMethod(); }公共类第一:IFirst
{
    公共无效FirstMethod(){Console.WriteLine(第一); }
}公共类第二:ISecond
{
    公共无效SecondMethod(){Console.WriteLine(二); }
}公共类FirstAndSecond:IFirst,ISecond
{
    首先第一个新=第一();
    二秒=新二();
    公共无效FirstMethod(){first.FirstMethod(); }
    公共无效SecondMethod(){second.SecondMethod(); }
}

我每次添加一个方法,我需要改变类的 FirstAndSecond 的,以及其中一个接口。

有没有办法来注入多个现有的类到一个新的类像它在C ++中是可能的?

也许有使用某种code一代的解决方案?

或者,它可能看起来像这样(假想的C#语法):

 公共类FirstAndSecond:IFirst从第一,从ISecond二
{}

这样不会有必要更新的类FirstAndSecond当我修改接口之一


修改

也许这将是更好地考虑一个实际的例子:

您有一个现有的类(例如基于ITextTcpClient一个基于文本的TCP客户端),你就已经在你的项目中的不同位置使用。现在,你觉得有必要创建类的组件很容易访问Windows窗体开发者。

据我所知您目前有两种方法可以做到这一点:


  1. 编写一个从继承的组件,并实现了TextTcpClient类使用类本身的实例界面如图所示FirstAndSecond一个新的类。


  2. 编写从TextTcpClient继承一个新的类并以某种方式实现IComponent的(实际上还没有试过这种尚未)。


在你需要做的每方法,而不是每个班级工作两种情况。既然你知道,我们需要TextTcpClient和组件的所有方法,这将是刚刚合并这两个为一类最简单的解决方案。

要避免冲突,这可能是由code一代来完成,其中的结果可能在事后改变,而​​是手动输入,这是在屁股痛纯


解决方案

  

由于多重继承是坏(它使源更复杂)C#不直接提供这样的模式。但有时这将是有帮助的这种能力。


C#和.NET CLR还没有实现心肌梗死,因为他们没有得出结论如何将C#,VB.net和其他语言之间的互操作呢,不是因为它将使来源更为复杂的

MI是一个有用的概念,未回答的问题是那些诸如? - 当你有不同的超多公共基类,你做什么

Perl是只有我曾经使用过其中MI作品和良好的工作语言。 .NET可能介绍它,​​但却不愿然而,CLR也已经支持MI但正如我已经说过了,没有语言结构为它超出了呢。

在那之前你被卡住代理对象和多个接口来代替:(

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

For instance I'm able to implement the missing multiple inheritance pattern using interfaces and three classes like that:

public interface IFirst { void FirstMethod(); }
public interface ISecond { void SecondMethod(); }

public class First:IFirst 
{ 
    public void FirstMethod() { Console.WriteLine("First"); } 
}

public class Second:ISecond 
{ 
    public void SecondMethod() { Console.WriteLine("Second"); } 
}

public class FirstAndSecond: IFirst, ISecond
{
    First first = new First();
    Second second = new Second();
    public void FirstMethod() { first.FirstMethod(); }
    public void SecondMethod() { second.SecondMethod(); }
}

Every time I add a method to one of the interfaces I need to change the class FirstAndSecond as well.

Is there a way to inject multiple existing classes into one new class like it is possible in C++?

Maybe there is a solution using some kind of code generation?

Or it may look like this (imaginary c# syntax):

public class FirstAndSecond: IFirst from First, ISecond from Second
{ }

So that there won't be a need to update the class FirstAndSecond when I modify one of the interfaces.


EDIT

Maybe it would be better to consider a practical example:

You have an existing class (e.g. a text based TCP client based on ITextTcpClient) which you do already use at different locations inside your project. Now you feel the need to create a component of your class to be easy accessible for windows forms developers.

As far as I know you currently have two ways to do this:

  1. Write a new class that is inherited from components and implements the interface of the TextTcpClient class using an instance of the class itself as shown with FirstAndSecond.

  2. Write a new class that inherits from TextTcpClient and somehow implements IComponent (haven't actually tried this yet).

In both cases you need to do work per method and not per class. Since you know that we will need all the methods of TextTcpClient and Component it would be the easiest solution to just combine those two into one class.

To avoid conflicts this may be done by code generation where the result could be altered afterwards but typing this by hand is a pure pain in the ass.

解决方案

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

C# and the .net CLR have not implemented MI because they have not concluded how it would inter-operate between C#, VB.net and the other languages yet, not because "it would make source more complex"

MI is a useful concept, the un-answered questions are ones like:- "What do you do when you have multiple common base classes in the different superclasses?

Perl is the only language I've ever worked with where MI works and works well. .Net may well introduce it one day but not yet, the CLR does already support MI but as I've said, there are no language constructs for it beyond that yet.

Until then you are stuck with Proxy objects and multiple Interfaces instead :(

这篇关于在C#中的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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