为什么我不能打电话,明确实现接口的类中的方法? [英] Why can't I call methods within a class that explicitly implements an interface?

查看:115
本文介绍了为什么我不能打电话,明确实现接口的类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的故事。我创建了一个接口, IVehicle 。我明确实现的接口在我的课, Vehicle.cs

Here's the story. I created an interface, IVehicle. I explicitly implemented the interface in my class, Vehicle.cs.

下面是我的界面:

Interface IVehicle
{
        int getWheel();
}



这是我的类:

here is my class:

class Vehicle: IVehicle
{

     public int IVehicle.getWheel()
     {
         return wheel;
     }

     public void printWheel()
     {
         Console.WriteLine(getWheel());
     }
}

请注意,getWheel()明确落实。现在,当我尝试的呼叫的我Vehicle类内的方法,我收到指示getWheel()不会在当前的背景下存在的错误。有人能帮助我理解我在做什么错了?

Notice that "getWheel()" is explicitly implemented. Now, when I try to call that method within my Vehicle class, I receive an error indicating that getWheel() does not exist in the current context. Can someone help me understand what I am doing wrong?

推荐答案

当你明确地实现接口,你首先必须把对象到接口,那么你可以调用该方法。换句话说,当该方法被调用的对象的接口类型上,而不是作为具体类型的方法只适用。

When you explicitly implement the interface, you first have to cast the object to the interface, then you can call the method. In other words, the method is only available when the method is invoked on the object as the interface type, not as the concrete type.

class Vehicle: IVehicle {

     public int IVehicle.getWheel()
     {
         return wheel;
     }

     public void printWheel()
     {
         Console.WriteLine( ((IVehicle)this).getWheel() );
     }
}

请参阅本的reference 的MSDN上了解更多信息。下面是相关的代码片段:

See this reference at MSDN for more information. Here's the relevant snippet:

这是不可能通过其全名来访问一个方法调用显式接口成员实现,属性访问或索引访问。显式接口成员实现只能通过接口实例来访问,并且在这种情况下,其成员名称引用。

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

有关这是非常值得 - 这可能不是一个特别好的使用显式接口实现的。通常情况下,你要使用显式实现,当你有一个具有典型操作的全接口的类也实现了可能会取代其中的一些操作的界面。典型的例子是文件类,它实现的IDisposable 。这将有一个关闭()的方法,但需要执行的Dispose()。当作为处理文件你可以使用打开/关闭。当在using语句打开,但是,它将把它作为一个的IDisposable 和呼叫的Dispose 。在这种情况下的Dispose 只需调用关闭。你不一定要暴露的Dispose 作为一部分文件的实现,因为同样的行为可以从关闭

For what it's worth -- this probably isn't a particularly good use of explicit interface implementation. Typically, you want to use explicit implementation when you have a class that has a full interface for typical operations but also implements an interface that may supersede some of those operations. The canonical example is a File class that implements IDisposable. It would have a Close() method but be required to implement Dispose(). When treating as a File you would use Open/Close. When opened in a using statement, however, it will treat it as an IDisposable and call Dispose. In this case Dispose simply calls Close. You wouldn't necessarily want to expose Dispose as part of the File implementation since the same behavior is available from Close.

这篇关于为什么我不能打电话,明确实现接口的类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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