简体   繁体   English

C# 如何在实现者 class 中引用默认接口实现

[英]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.

相关问题 C#中如何使用接口默认实现 - How to use interface default implementation in C# C#:我可以指定一个类的成员结构作为继承接口的实现者吗? - C#:Can I designate a class' member struct to be the implementer of an inherited interface? 如何在接口实现代码C#之前执行一个默认方法? - How to execute a default method before the interface implementation code C#? C#调用接口方法默认实现 - C# call interface method with default implementation C# class 可以从自己的实现中调用接口的默认接口方法吗? - Can a C# class call an interface's default interface method from its own implementation? C# 8 默认接口实现与继承 - C# 8 default interface implementation and inheritance 如何在C#中将接口实现委托给其他类 - How to delegate interface implementation to other class in C# Java 可以结合抽象 class 的默认实现和 class 中的接口。有没有办法在 C# 中实现这一点? - Java can combine default implementation from an abstract class and interface in a class. Is there a way to achieve this in C#? C#类实现中接口变量的用途 - Purpose of interface variable in C# class implementation C#泛型接口实现到派生类 - C# generic Interface implementation to derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM