实现“继承”(实现)通用接口的接口?(Implementing Interfaces That “Inherit” (Implement) A Common Interface?)

编程入门 行业动态 更新时间:2024-10-23 15:19:08
实现“继承”(实现)通用接口的接口?(Implementing Interfaces That “Inherit” (Implement) A Common Interface?) interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle { void UseKatana(); } interface IRaphael : ITurtle { void UseSai(); } interface IDonatello : ITurtle { void UseBo(); } interface IMichelangelo : ITurtle { void UseNunchuku(); }

如果我想创建一个可以完成所有4个的大乌龟怎么办? 我想编码:

class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo { // Implementation not shown for brevity. }

这是可能的,因为现在,似乎我必须每次实施4次Fight()和EatPizza() 。 但我认为这两个常用功能将解决,只需要实施一次,对吧?

我可以创建4个中间接口而不继承ITurtle ,然后让GrandTurtle实现ITurtle 。 这解决了接口继承问题,但现在它看起来在语义上是错误的,因为它使得ITurtle看起来像第5个兄弟而不是它。 另外,我希望能够创建特定于海龟的类,例如, class BostonLeonardo : ILeonardo 。

我从许多地方读过,似乎是一场无休止的辩论 - 有人说“接口内的继承”很精细,而那些说不是 - 我要么不理解他们的解释,要么他们只是说这是不好的做法而不解释为什么。

interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle { void UseKatana(); } interface IRaphael : ITurtle { void UseSai(); } interface IDonatello : ITurtle { void UseBo(); } interface IMichelangelo : ITurtle { void UseNunchuku(); }

What if I want to create a grand turtle that can do all 4? I want to code:

class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo { // Implementation not shown for brevity. }

Is this possible because now, it seems like I'd have to implement Fight() and EatPizza() 4 times each. But I think those two common functions will resolve and would only need to be implemented once, right?

I could have created the 4 intermediate interfaces without inheriting ITurtle, and then have GrandTurtle implement ITurtle. This solves the interface inheritance issue but now it looks semantically wrong because it makes ITurtle look like a 5th brother which it's not. Plus, I want to be able to create turtle-specific classes, for example, class BostonLeonardo : ILeonardo.

I've read from many places and it seems like an endless debate - some say "inheritance within interfaces" is perflectly fine, and those that say it's not - either I don't understand their explanation or they just say it's bad practice without explaining why.

最满意答案

您只能实现Fight和EatPizza方法一次,因为只有一个接口定义它们。 如果你在每个ILeonardo等接口上都​​有Fight和EatPizza ,你可以选择实现它们或者使用显式接口实现来改变每个接口签名的那些方法的行为 。 我会做一个例子,因为我喜欢TMNT:

interface ILeonardo { void Fight(); void EatPizza(); void UseKatana(); } interface IRaphael { void Fight(); void EatPizza(); void UseSai(); } interface IDonatello { void Fight(); void EatPizza(); void UseBo(); } interface IMichelangelo { void Fight(); void EatPizza(); void UseNunchuku(); } class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo { // Code that fires when Fight is called on ILeonardo turtle = new GrandTurtle() void ILeonardo.Fight() { UseKatana(); } // Code that fires when Fight is called on IRaphael turtle = new GrandTurtle() void IRaphael.Fight() { UseSai(); } // Code that fires for all other turtles public void Fight() { UseThatCrappyStickThingTheOldActionFiguresCameWith(); } // Implement EatPizza() and such here... }

只有当GrandTurtle的类型签名是适当的接口时,这些显式接口实现才会生效。

编辑:对不起,我的回答最初是错的; 我一直在喝酒,TMNT让我很兴奋

You can only implement the methods Fight and EatPizza once because only one of the interfaces defines them. If you had Fight and EatPizza on each of the ILeonardo etc. interfaces, you could choose to implement them once OR use explicit interface implementations to change the behavior of those methods per interface signature. I'll do an example because I love TMNT:

interface ILeonardo { void Fight(); void EatPizza(); void UseKatana(); } interface IRaphael { void Fight(); void EatPizza(); void UseSai(); } interface IDonatello { void Fight(); void EatPizza(); void UseBo(); } interface IMichelangelo { void Fight(); void EatPizza(); void UseNunchuku(); } class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo { // Code that fires when Fight is called on ILeonardo turtle = new GrandTurtle() void ILeonardo.Fight() { UseKatana(); } // Code that fires when Fight is called on IRaphael turtle = new GrandTurtle() void IRaphael.Fight() { UseSai(); } // Code that fires for all other turtles public void Fight() { UseThatCrappyStickThingTheOldActionFiguresCameWith(); } // Implement EatPizza() and such here... }

These explicit interface implementations would take effect only when the type signature of GrandTurtle is the appropriate interface.

EDIT: sorry that my answer was initially wrong; I've been drinking and TMNT excites me

更多推荐

本文发布于:2023-07-29 20:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   Interfaces   Implementing   Inherit   Common

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!