(C#)与虚方法的接口

时间:2017-12-21 09:26:29

标签: c# inheritance interface virtual

我需要一个接口对象IMyInterface obj;,它可以吞下从它继承的所有类对象,因为我的示例代码运行良好但我需要实现函数我不需要因为IMyInterface需要它,我尝试了这两种方式,但都失败了:

  1. 创建virtual class MethodThatNotBelongToUsAll{}并尝试class ClassA_wrap : ClassA, MethodThatNotBelongToUsAll, IMyInterface但收到错误class 'ClassA_wrap' cannot have multiple base classes

  2. interface IMyInterface更改为virtual class IMyInterface{},但第obj = new ClassA_wrap();行显示错误cannot implicitly convert type "ClassA_wrap" to "IMyInterface"

  3. 任何人都可以帮我这个吗? 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();
            }
        }
    

1 个答案:

答案 0 :(得分:3)

在此处阅读:Interface Segregation Principle

我认为您需要查看SOLID之一I: interface segregation principle

这个原则说如果你在接口中有方法就创建单独的interce,你不会实现它。

我建议像这样设计

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 
}