(C#)接口与虚方法

(C#)interface with virtual method

我需要一个接口对象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"

谁能帮我解决这个问题?谢谢!

    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();
        }
    }

在这里阅读:Interface Segregation Principle

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

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

我建议这样设计

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 
}