Your Math class needs some small adaptions to function like you want.
public class Math : IMath1,IMath2
{
int x = 5;
int y= 6;
private int z;
private bool oneOrTwo = true; //True = 1, False = 2
public Math(bool oneOrTwo)
{
this.oneOrTwo = oneOrTwo;
z = GetSum(x, y);
}
public int GetSum(int x, int y)
{
return oneOrTwo ? ((IMath2)this).GetSum(x, y) : ((IMath1)this).GetSum(x, y);
}
}
With the Boolean I decide from witch interface the GetSum
should be used and I give z it's value in the constructor, otherwise the GetSum
method would need to be static.
And you need to cast this to the interface, otherwise it doesn't know that it has these methods.
If you need to implement the methods in your class, then do this.
public int GetSum(int x, int y)
{
return oneOrTwo ? ((IMath1)this).GetSum(x, y) : ((IMath2)this).GetSum(x, y);
}
int IMath1.GetSum(int x, int y)
{
return x + y;
}
int IMath2.GetSum(int x, int y)
{
return x * y;
}