开发者

C# Selected Implementation of Interface

Is there a way to select methods to be implemented in an interface? I thin开发者_如何学编程k in Java they have adapter to only select what method you want.


There isn't a way to pick-and-choose what you implement. You can, however, from good to bad to worse...

good - break the interface up into multiple interfaces - your classes can pick and choose what interfaces they should inherit from based off of what functionality they should provide (your classes can inherit from as many interfaces as you want)

bad - throw a NotImplementedException

worse - silently do nothing

Do you have a need to do this or is it curiosity? It sounds like you're laying the foundation for a very confusing API if you're destroying the value of interfaces. They define contracts that callers rely on.


The point of an interface is to say "Hey, anything that implements me is guaranteed to have all of the members that I declare!"

If you want anything other than that, then you don't want an interface, or you want an interface that declares only method1, in your example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜