Reputation: 501
I need a interface objectIMyInterface obj;
that can swallow all classes object that inherit from it, as my sample code that are working well but inside I need implement function I don't need because IMyInterface
require it, I try these two ways but both failed:
create virtual class MethodThatNotBelongToUsAll{}
and try class ClassA_wrap : ClassA, MethodThatNotBelongToUsAll, IMyInterface
but receive error class 'ClassA_wrap' cannot have multiple base classes
change interface IMyInterface
to virtual class IMyInterface{}
but the line obj = new ClassA_wrap();
show error cannot implicitly convert type "ClassA_wrap" to "IMyInterface"
can anyone help me with this? thx!
interface IMyInterface
{
int Foo0 { get; } //ClassA,B,C method
int FooWrap(int F); //ClassA_wrap,B_wrap,C_wrap method
int FooA(int F); // ClassA method
int FooB(int F); // ClassB method
int FooC(int F); // ClassC method
}
class ClassA //Base Class, can't edit
{
public int Foo0{ get { return 1; } }
public int FooA(int F) { return F; }
}
class ClassB //Base Class, can't edit
{
public int Foo0 { get { return 2; } }
public int FooB(int F) { return F; }
}
class ClassC //Base Class, can't edit
{
public int Foo0 { get { return 3; } }
public int FooC(int F) { return F; }
}
class ClassA_wrap : ClassA, IMyInterface
{
public int FooB(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooC(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooWrap(int F)
{
return FooA(F)*10+1;
}
}
class ClassB_wrap : ClassB, IMyInterface
{
public int FooA(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooC(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooWrap(int F)
{
return FooB(F)*20+2;
}
}
class ClassC_wrap : ClassC, IMyInterface
{
public int FooA(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooB(int F) { return -1; } // I want to get rid of this line, but Interface require to imp this...
public int FooWrap(int F)
{
return FooC(F)*30+3;
}
}
class MainClass
{
static void Main()
{
IMyInterface obj; //I need IMyInterface object that can swallow all three classes depend on flag
int flag = 2; // or 1 or 3
if(flag==1)
obj = new ClassA_wrap();
else if(flag==2)
obj = new ClassB_wrap();
else
obj = new ClassC_wrap();
//-----------------------------------------
Console.WriteLine( obj.Foo0);
//-----------------------------------------
if(obj is ClassA_wrap)
Console.WriteLine(obj.FooA(11));
if (obj is ClassB_wrap)
Console.WriteLine(obj.FooB(22));
if (obj is ClassC_wrap)
Console.WriteLine(obj.FooC(33));
//-----------------------------------------
Console.WriteLine(obj.FooWrap(1));
Console.Read();
}
}
Upvotes: 2
Views: 156
Reputation: 176956
Read it over here : Interface Segregation Principle
i think you need to look one of SOLID
whichis I: interface segregation principle
this principle says that create seperate interce if you are having method in interface, which you calss is not going to implement.
i suggest design like this
public interface IMasterInterface
{
//contains all common method
int Foo0 { get; } //ClassA,B,C method
int FooWrap(int F); //ClassA_wrap,B_wrap,C_wrap method
}
public interface IFooA
{
//contians method related to A only
int FooA(int F); // ClassA method
}
public interface IFooB
{
//contians method related to B only
int FooB(int F); // ClassA method
}
public class A : IFooA,IMasterInterface
{
//common method
//now this will have method related to A only7
}
public class B: IFooB,IMasterInterface
{
//common method
//now this will have method related to B only
}
Upvotes: 3