我在Class中有实现接口(interface)的方法。当我实现显式实现时,出现编译器错误

The modifier 'public' is not valid for this item

为什么不允许将public用于显式接口(interface)实现?

最佳答案

显式接口(interface)实现的原因是避免名称冲突,最终结果是在调用这些方法之前必须将对象显式转换为该接口(interface)。

您可以认为这些方法不是在类上公开的,而是直接与接口(interface)绑定(bind)的。没有必要指定public/private/protected,因为接口(interface)不能具有非public成员,所以它将始终是public。

(微软有一个overview on explicit interface implementation)

10-07 18:54
查看更多