如何从扩展到接口的类实现方法?
我有这个界面:
public Interface myInterface
{
public static int myMethod();
}
Run Code Online (Sandbox Code Playgroud)
而这堂课:
public class MyClass : myInterface
{
// And I want here to implement the method form myInterface and i don't know how
}
Run Code Online (Sandbox Code Playgroud)
Interface将无法编译 - 将其更改为interface.
static不能成为的一部分interface.
更正后,代码看起来像这样:
public interface myInterface
{
int myMethod();
}
public class MyClass : myInterface
{
public int myMethod()
{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
关于static并interface查看来自http://channel9.msdn.com/forums/TechOff/250972-Static-Inheritance/?CommentID=274217的报价:
.NET中的继承仅适用于实例库.静态方法是在类型级别上定义的,而不是在实例级别上定义的.这就是为什么覆盖不适用于静态方法/属性/事件...
静态方法只在内存中保存一次.没有为它们创建的虚拟表等.
如果在.NET中调用实例方法,则始终为其提供当前实例.这是.NET运行时隐藏的,但它确实发生了.每个实例方法都将第一个参数作为运行该方法的对象的指针(引用).静态方法不会发生这种情况(因为它们是在类型级别定义的).编译器应该如何决定选择要调用的方法?