为什么我不能在显式实现接口的类中调用方法? [英] Why can't I call methods within a class that explicitly implements an interface?

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

问题描述

这是故事.我创建了一个界面,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();
}

这是我的课:

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() );
     }
}

请参阅此参考 在 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.

就其价值而言——这可能不是对显式接口实现的特别好的使用.通常,当您的类具有典型操作的完整接口但还实现了可能取代其中一些操作的接口时,您希望使用显式实现.规范示例是实现 IDisposableFile 类.它将有一个 Close() 方法,但需要实现 Dispose().当处理为 File 时,您将使用 Open/Close.但是,当在 using 语句中打开时,它会将其视为 IDisposable 并调用 Dispose.在这种情况下,Dispose 只需调用 Close.您不一定希望将 Dispose 作为 File 实现的一部分公开,因为相同的行为可从 Close 获得.

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天全站免登陆