带接口的多重继承 [英] Multiple Inheritance with Interface

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

问题描述

大家好....



我在以下代码块中有点混乱。任何人都可以帮助我。



  interface  i1 
{
void mult();
}

interface i11
{
void mult();
}

class myClass:i1,i11
{

public void mult()
{
Console.Write( test);
}
}

class 计划
{
static void Main( string [] args)
{
myClass obj = new myClass();
obj.mult();
}
}





此代码成功执行,没有任何错误,并将输出生成为test。 />
我的查询是编译器如何知道它正在执行哪种接口方法。

for I1或I11


请在此建议。



谢谢

Vamsi krishna

解决方案

< blockquote>这里有相同参数和返回类型的方法。

你正在调用类的方法而不是接口。



接口只包含方法,属性,事件或索引器的签名。 -  MSDN 





所以界面只是为了签名。

在你的情况下编译器只检查那个类实现接口的人是否实现了它的所有方法。调用特定接口的方法无关紧要。



一般我们在多态性方面使用接口并采取接口亲子参考相关优势。



这里我回答了一个问题,显示了使用界面的实时例子。



在多态性中是什么使用父类引用变量指向子类对象实例的实际用法? [ ^ ]



我希望这可能清除你的想法。



希望这有助于。

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

Pratik Bhuva


< blockquote>它没有。它为两者调用相同的方法。

如果你需要单独的方法,那么明确地实现它们。

 class myClass:i1,i11 
{

public void i1.mult()
{
Console.Write(i1 test);
}
public void i11.mult()
{
Console.Write(i11 test);
}
}


Hi All....

I am bit confusing in the following code block. Could any one help me in this.

interface i1
    {
         void mult();
    }

    interface i11
    {
        void mult();
    }

    class myClass:i1,i11
    {

     public void mult()
        {
            Console.Write("test");
        }
    }

  class Program
    {
        static void Main(string[] args)
        {
            myClass obj= new myClass();
            obj.mult();
          }
    }



This code executes successfully without any errors and produce output as "test".
My Query in this is How the Compiler has the idea for which interface method it is executing.
for I1 or I11
?
Please suggest on this.

Thanks
Vamsi krishna

解决方案

here there are methods with same parameter and return type.
and You are calling method of class not of interface.

"An interface contains only the signatures of methods, properties, events or indexers." - MSDN



So interface is just for signature.
In your case Compiler will only check that the class who implement the interface implements its all methods or not.there is no concern with calling a method of particular interface.

Generally we use interface in terms of polymorphism and to take Parent-Child reference related advantages.

Here i have answer one question which shows the real time example of use of Interface.

In polymorphism what is the practical use of using a parent class reference variable to point to child class object instance?[^]

I hope this may clear your idea.

Hope This Helps.
------------------
Pratik Bhuva


It doesn't. It calls the same method for both.
If you need separate method, then explicitly implement them.

class myClass:i1,i11
{

 public void i1.mult()
    {
        Console.Write("i1 test");
    }
 public void i11.mult()
    {
        Console.Write("i11 test");
    }
}


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

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