[英]C# How to reference default interface implementation in implementer class
Consider the following interface, with a default implementation of TestMethod
考虑以下接口,默认实现
TestMethod
public interface TestInterface
{
public int TestMethod()
{
return 15;
}
}
Calling TestMethod
in the following class will cause a StackOverflowException:在下面的 class 中调用
TestMethod
会导致 StackOverflowException:
public class TestClass : TestInterface
{
public int TestMethod()
{
return 1 + (this as TestInterface).TestMethod();
}
}
Now I understand why this is, but is there any way to get around it?现在我明白为什么会这样了,但是有什么办法可以绕过它吗? Something like
base.TestMethod()
for referencing one of the class's implemented interfaces?类似
base.TestMethod()
的东西用于引用类的实现接口之一?
I know I could rename the method in TestInterface and reference it in TestClass that way, but that would cause problems for other classes that don't need to reference the default implementation.我知道我可以重命名 TestInterface 中的方法并以这种方式在 TestClass 中引用它,但这会导致其他不需要引用默认实现的类出现问题。
you need to use "public override" to do what you are asking.您需要使用“公共覆盖”来执行您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.