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.