[英]C# Static method in interface cannot be accessed from the implementing class
The method SomeStaticMethod
from interface IA
is not directly accessible from the class A
that implemented the interface.接口
IA
中的方法SomeStaticMethod
不能直接从实现该接口的 class A
访问。 Am I missing something?我错过了什么吗?
public interface IA {
public static int SomeStaticMethod() => 4;
}
public class A : IA {
public static void Bidule() {
SomeStaticMethod(); //DO NOT COMPILE
IA.SomeStaticMethod(); //COMPILES
}
}
If the interface IA
is changed to a class, it works.如果接口
IA
更改为 class,它可以工作。
public class IA {
public static int SomeStaticMethod() => 4;
}
public class A : IA {
public static void Bidule() {
SomeStaticMethod(); //NOW it works
}
}
A class can implement multiple interfaces.一个 class 可以实现多个接口。 If you defined a static method with the same signature in both interfaces, the compiler couldn't decide which of those should be invoked.
如果您在两个接口中定义了具有相同签名的 static 方法,则编译器无法决定应该调用哪一个。 It's one of the (many) reasons why most OO languages try to avoid multiple-inheritance, useful as it can be.
这是大多数 OO 语言试图避免多重继承的(许多)原因之一,尽管它可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.