显式接口实现,为什么显式转换 [英] explicit interface implementation, why explicit casting

查看:132
本文介绍了显式接口实现,为什么显式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个类明确地实现你为什么需要一个接口为明确投类的实例,以便使用实现的方法接口?



MSDN:显式接口实现(这个例子就是从这里取一>)



您有两个接口如下:

 接口的iControl 
{
无效漆();
}
接口ISurface
{
无效漆();
}

和你明确地实现它们。

 公共类SampleClass:iControl为ISurface 
{
无效的IControl.Paint()
{
的System.Console.WriteLine (的IControl.Paint);
}
无效ISurface.Paint()
{
的System.Console.WriteLine(ISurface.Paint);
}
}

现在,使用你有下面的代码接口

  //调用从主画图的方法。 

SampleClass的obj =新SampleClass();
//obj.Paint(); //编译器错误。

的iControl C =(的iControl)目标文件;
c.Paint(); //拜会SampleClass的IControl.Paint。

ISurface S =(ISurface)目标文件;
s.Paint(); //拜会SampleClass ISurface.Paint。

在上面的代码块,你为什么

 的iControl C =(的iControl)目标文件; 



而不是

 的iControl C = OBJ; 





原因我的困惑是因为,例如,你可以做以下

 的IDictionary<字符串,字符串> C =新词典<字符串,字符串>(); 



但没有明确铸造新词典的IDictionary



感谢。


解决方案

当一个类明确地实现你为什么需要显式转换类的实例,以便使用实现的方法?


接口的接口

成员有效上不存在类,至于编译器的关注 - 它的只有的存在的接口。你不必虽然显式转换 - 你只需要拥有它具有界面的编译时类型的参考。那然而,可以做你喜欢的,包括隐式转换。




在上面的代码块,你为什么

 的iControl C =(的iControl)目标文件; 



而不是

 的iControl C = OBJ; 




您不必。隐式转换应该是绝对的罚款。您的将会的有才能调用该方法在一个表达式显式转换,如:

  OBJ 。涂料(); //无效
((的iControl)OBJ).Paint(); //有效



但是,如果你通过任务去通过隐式转换为界面的独立本地变量类型,这很好 - 方法仍然被称为一个目标表达式是接口类型的


When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

(This example is taken from here: MSDN: Explicit Interface Implementation)

You have two interfaces as follows.

interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}

And you implement them explicitly.

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

Now, to use the interfaces you have the following code.

// Call the Paint methods from Main.

SampleClass obj = new SampleClass();
//obj.Paint();  // Compiler error.

IControl c = (IControl)obj;
c.Paint();  // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass. 

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

?

The reason for my confusion is because, for example, you can do the following

IDictionary<string, string> c = new Dictionary<string, string>();

without explicitly casting new Dictionary to IDictionary.

Thanks.

解决方案

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

The member effectively doesn't exist on the class, as far as the compiler's concerned - it only exists on the interface. You don't have to explicitly cast though - you just have to have a reference which has a compile-time type of the interface. That can be done however you like, including implicit conversions.

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

You don't have to. The implicit conversion should be absolutely fine. You would have to cast explicitly in order to call the method in a single expression, e.g.

obj.Paint(); // Invalid
((IControl) obj).Paint(); // Valid

But if you go through an implicit conversion via assignment to a separate local variable of the interface type, that's fine - the method is still being called with a target expression which is of the interface type.

这篇关于显式接口实现,为什么显式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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