C# 接口——只在其他接口中实现一个接口 [英] C# Interfaces- only implement an interface in other interfaces
本文介绍了C# 接口——只在其他接口中实现一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在其他接口中实现某些接口,我不希望它们能够被类直接继承.
提前致谢!
解决方案
你不能在 C# 中这样做 - 任何类都可以实现它有权访问的任何接口.
你为什么要这样做?请记住,通过声明接口继承:
公共接口 InterfaceA {}公共接口 InterfaceB : InterfaceA {}
您指定任何实现 InterfaceB
的东西也必须实现 InterfaceA
,所以无论如何你都会得到实现 InterfaceA
的类.>
I would like to only implement certain interfaces within other interfaces, I don't want them to be able to be inherited directly by a class.
Thanks in advance!
解决方案
You can't do this in C# - any class can implement any interface it has access to.
Why would you want to do this? Bear in mind that by declaring an interface inheritance:
public interface InterfaceA {}
public interface InterfaceB : InterfaceA {}
You're specifying that anything implementing InterfaceB
also has to implement InterfaceA
, so you'll get classes implementing InterfaceA
anyway.
这篇关于C# 接口——只在其他接口中实现一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文