【发布时间】:2021-11-22 18:03:54
【问题描述】:
考虑以下接口,默认实现为TestMethod
public interface TestInterface
{
public int TestMethod()
{
return 15;
}
}
在下面的类中调用TestMethod会导致StackOverflowException:
public class TestClass : TestInterface
{
public int TestMethod()
{
return 1 + (this as TestInterface).TestMethod();
}
}
现在我明白为什么会这样了,但是有什么办法可以绕过它吗? base.TestMethod() 之类的东西用于引用该类的已实现接口之一?
我知道我可以重命名 TestInterface 中的方法并以这种方式在 TestClass 中引用它,但这会导致其他不需要引用默认实现的类出现问题。
【问题讨论】:
标签: c# interface default-implementation