问题描述
当我需要显式地实现接口成员,它是私有
例如:
//当显式实现它始终是私人
无效IPointy.Draw()
{
}
为什么这样设计规则强制执行
请注意:
当具有相同的方法,以避免冲突,我必须明确地实现两个接口是
班线:IPointy
{
//现在是私人
无效IPointy.Draw()
{
}
}
我的问题是执行的原因是私有的。
显式实现接口成员不只是私人的。他们是公众 - 样的
他们在能投的参考接口可以打电话给他们任何代码公开。 (如果接口本身是不公开的,那么我想你可以说他们有效地具有相同的访问级别的接口。)
他们没有任何指定的有访问级别,因为他们的有无的是公共的接口方面:有没有参与的选择。他们以同样的方式作为一种正常的公众成员不是公共的成员,但他们从任何其他汇编语言调用它可以得到一个参考的保留,将它转换为接口类型...
在C#3.0规范中是这样说的:
When i need to implement the interface member explicitly ,it is private.
for example :
// when explicit implementation it is always private
void IPointy.Draw( )
{
}
why such design rule is enforced ?
Note :
When two interfaces having same methods ,to avoid the clash i have to explicitly implement is as
class Line :IPointy
{
// Now it is private
void IPointy.Draw( )
{
}
}
My question is the reason for implementing is as private.
Explicitly implemented interface members aren't simply private. They're public - sort of.
They're public in that any code which can cast the reference to the interface can call them. (If the interface itself isn't public, then I guess you could say they effectively have the same access level as the interface.)
They don't have any specified access level because they have to be public in terms of the interface: there's no choice involved. They're not public members in the same way as normal public members of a type, but they're callable from any other assembly which can get hold of a reference and cast it to the interface type...
The C# 3.0 specification puts it this way:
这篇关于C# - 什么是"的原因;明确实现的接口成员总是隐含私人"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!