You are trying to implement diamond inheritance using interfaces. For all reason, you are not allowed to implement several classes, primarily to avoid the inheritance of diamonds.
If you want to combine the two interfaces together as ComplexAction , you would do something like the following:
interface IAct { bool CanAct(); } class Act1 : IAct { public bool CanAct() { return true; } } class Act2 : IAct { public bool CanAct() { return false; } } class ComplexAction : IAct { private Act1 action1; private Act2 action2; public ComplexAction(Act1 action1, Act2 action2) { this.action1 = action1; this.action2 = action2; } public bool CanAct() { return action1.CanAct() && action2.CanAct(); } }
A ComplexAction is a composition with various IAct s. If you add a number to the interface name, the likelihood that you are doing something wrong is likely.
If instead you want to define other behavior based on an interface, that interface should define its method on its own.
interface IAct1 { bool CanAct(); } interface IAct2 { bool CanAct(); } class SometimesAct1SometimesAct2 : IAct, IAct1, IAct2 { bool IAct1.CanAct() { return false; } bool IAct2.CanAct() { return true; } public bool CanAct() { Console.WriteLine("Called on IAct or SometimesAct1SometimesAct2"); return false; } }
To avoid the problems of diamond inheritance, you must provide an implementation for ALL interfaces that define a specific method, so there is no ambiguity.
Josh Smeaton
source share