如何使用接口进行多重继承是一种纯粹的多重继承方式 [英] How does multiple inheritance using interfaces is a pure way of multiple inheritance

查看:101
本文介绍了如何使用接口进行多重继承是一种纯粹的多重继承方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们多次听说过dot支持多点继承。我在Google上搜索一些相同的例子。为了实现这一点,我发现有一种Helper类可以实现2个接口。对于接口成员,我们在helper类中提供body。我们在helper类中创建了2个类的对象,并将object.method()传递给接口方法的方法体。然后我们在实现类中创建辅助类的对象,并从中访问其他2个类的方法。我的问题是,当我们在内部创建2个类的对象时创建辅助类的对象和辅助类对象时,为什么不在实现类中创建2个类的对象。



我尝试过的事情:



We have heard many times that multiple inheritance in dot net is supported by interfaces. I was googling for some examples of the same. To Achieve this, I found that a kind of Helper class is made which implements 2 interfaces. And to the interface members we provide body in helper class. We create object of 2 classes in helper class and we pass object.method() to the method body of interface methods. Then we create object of helper class in implementing class and from that we access the methods of 2 other classes. My Question is, When we create object of helper class and that helper class object in internally creating object of 2 classes, Why not we create the object of 2 classes in the implementing class.

What I have tried:

I am what is the benefit of (A) when we can directly create object of 2 classes in third implementing class


--Section (A)---
<pre>namespace Learning
{
    interface IA
    {
         void MethodA();
    }
    interface IB
    {
         void MethodB();
    }
    class A : IA
    {
     public void MethodA()
        {
            Console.WriteLine("Hi This is method A");
        }
    }

    class B: IB
            {
     public  void MethodB()
        {
            Console.WriteLine("Hi This is method B");
        }
    }

   class HelperClass : IA, IB
    {
        A a = new A();
        B b = new B();

        public void MethodA()
        {
            a.MethodA();
        }
        public void MethodB()
        {
            b.MethodB();
 
        }

    }
    
    class Program
    {
        static void Main(string[] args)
        {

            HelperClass hc = new HelperClass();
            hc.MethodA();
            hc.MethodB();

            //Why Not below code
            //Why this is not the way to achieve multiple inheritance
            A a = new A();
            B b = new B();
            a.MethodA();
            b.MethodB();

         }
    }  
 
}

推荐答案

在你的例子中一个接口实现MethodA和另一个MethodB。现在想象两个接口都有MethodA,你的HelperClass如何实现MethodA?它会返回a.MethodA还是b.MethodA?怎么可能决定?这就是为什么你不能拥有多重继承的关键。



我们多次听说过dot网络支持多重继承。



首先,你不继承你实现它们的接口,其次.net确实允许你实现多个接口;



In your example one interface implements MethodA and the other MethodB. Now imagine both interfaces had MethodA, how would your HelperClass implement MethodA? Would it return a.MethodA or b.MethodA? How could it possibly decide? That's the crux of why you can't have multiple inheritance.

"We have heard many times that multiple inheritance in dot net is supported by interfaces."

First off you don't inherit interfaces you implement them, and secondly .net does allow you to implement multiple interfaces;

class MyClass : IMyIntefaceA, IMyInterfaceB





这不仅仅是迂腐,关于接口的事情是它没有实现(即没有代码)。在处理继承时,您正在处理具有实现的类。因此想象一个实现两个接口的类......它是一个实现两个接口的类,因此您需要将两个接口的代码放在一个类中。如果这两个接口实现了相同的方法,那么这不是问题,因为你的单个类只会实现一次方法,因此两个接口方法都将采用相同的具体方法来实现它们。



现在假设一个类继承了另外两个类......这两个类都有自己的实现(即代码)。现在让我们考虑这两个类都实现相同的方法,所以当在派生类上调用该方法时,调用哪个继承类的方法?框架是如何决定的?



你可以说你可以覆盖派生类中的方法并在那里做出决定,但是如果方法是在基类上调用虚方法和方法?现在是框架必须决定调用哪些类的方法以及它可能如何决定?出于这些原因,MS决定保持简单并且不支持多重继承。



This isn't just being pedantic, the thing about an interface is that it has no implementation (ie no code). When you are dealing with inheritance you are dealing with classes that do have implementation. So imagine a class that implements two interfaces…it is one class implementing two interfaces so you need to put the code for both interfaces in a single class. If those two interfaces implement the same method then it's not a problem because your single class will only implement the method once, so both interface methods will have the same concrete method implementing them.

Now imagine a class that inherits two other classes…both those classes have their own implementation (ie code). Now let's consider that both of those classes implement the same method, so when that method is called on the derived class which of the inherited classes' method is called? How does the framework decide?

You could argue that you could override the method in the derived class and make the decision there which is fine, but what if the methods are virtual methods and the method is called on a base class? It is now the framework that must decide which classes' method is called and how can it possibly decide? It's for these reasons that MS decided to keep things simple and not support multiple inheritance.


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#和.net CLR没有实现MI,因为他们还没有断定它如何在C#之间互操作, VB.net和其他语言,不是因为它会使源更复杂



MI是一个有用的概念,未回答的问题是: - 当你在不同的超类中有多个公共基类时,你会怎么做?



Perl是我工作过的MI工作和工作的唯一语言好吧.Net可能会介绍它有一天但尚未推出,CLR确实已经支持MI但正如我所说的那样,除此之外还没有语言结构。



在此之前,你会遇到代理对象和多个接口而不是


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


这篇关于如何使用接口进行多重继承是一种纯粹的多重继承方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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