实现Interface的方法不能使用static修饰符open in new window

Posted on 2004-03-24 10:15 dudu 评论(3) 收藏

今天在写程序时, 在一个方法前加了static, 而这个方法实现了interface中的一个成员。编译时,总是出现“不会实现接口成员”的错误,百思不得其解,后来将static去掉,编译就成功。

在www.asprelated.com上看到这么一段话:"It is a compile-time error for an explicit interface member implementation to include access modifiers, and it is a compile-time error to include the modifiers abstract, virtual, override, or static."

详细内容请看: http://www.asprelated.com/csharp/sharp-13_4.aspx。



1楼 2004-03-24 10:48 Meyer

你的接口不是使用的显示实现。关于显示实现接口的这一段话应该不足以解释这个编译时错误。
出错原因在于,实现接口方法都应该non-static方法。只有非静态方法才可能具备多态性。你加上了static还怎么实现接口的方法?

2楼 2004-03-24 11:19 dudu

你说的有道理。刚刚弄明白“显示实现接口”是怎么回事?
由此想到, C#中接口为什么不支持static成员, 请看这里的讨论:
http://www.dotnet247.com/247reference/msgs/41/207853.aspx

3楼 2004-03-24 11:33 Ninputer

如果你对显式实现感兴趣,可以看看这篇
VB关于接口实现的语法比C#更强大

http://dotnet.blogger.cn/ninputer/archive/2004/03/18/417.aspx

4楼 2008-05-26 01:21 KyLi

类型成员应该是公有的吧
显示接口成员有时公有,有时私有,取决于referrence的方式。
所以不能加modifier
没记错的话,
接口里的成员方法,
在C#里默认的是sealed virtual。
乱弹一下,呵呵,刚刚开始学习.net。

Contributors: FHL