关于INTERFACE的采访问题 [英] Interview Question about INTERFACE

查看:78
本文介绍了关于INTERFACE的采访问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在两个不同的界面(I1,I2)中有共同的方法,如何调用I2的方法?或者是否可以在不同的界面中创建相同的方法名称,请指导我任何一个...



在此先感谢.....

We have common method in two different interface(I1, I2), how to call method of I2 ? OR Is it possible to create same method name in different Interface, please guide me any one...

Thanks in Advance.....

推荐答案

是的。您所要做的就是通过强制转换访问相应的界面:

Yes. All you have to do is access it via a cast to the appropriate Interface:
public interface Ia
    {
    void Common();
    }
public interface Ib
    {
    void Common();
    }
public class MyClassImplicit : Ia, Ib
    {
    public void Common()
        {
        Console.WriteLine("Common");
        }
    }
public class MyClassExplicit : Ia, Ib
    {
    void Ia.Common()
        {
        Console.WriteLine("Common Ib");
        }
    void Ib.Common()
        {
        Console.WriteLine("Common Ia");
        }
    }




MyClassImplicit mci = new MyClassImplicit();
mci.Common();
MyClassExplicit mce = new MyClassExplicit();
((Ia)mce).Common();
((Ib)mce).Common();
// mce.Common();  //*** Will not compile if uncommented


是的,我们可以创建。

你可以看看这个链接。你将能够更好地理解。

http://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name-in-c-sharp [<一个href =http://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name-in-c-sharptarget =_ blanktitle =New Window > ^ ]
Yes we can create.
You can have a look here with this link. You will be able to understand better.
http://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name-in-c-sharp[^]


是的,你可以。



让我告诉你



Yes you can.

Let me show you

public interface IT1
{
 void Test();
}

public interface IT2
{
void Test();
}

public class C1 : IT1,IT2
{
void IT1.Test()
{
Console.Write("IT1 Implementation"); 
}
public void Test()
{
Console.Write("IT2 Implementation");
}
}





享受......



Enjoy...


这篇关于关于INTERFACE的采访问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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