(C#)与虚方法的接口

问题描述 投票:2回答:1

我需要一个接口objectIMyInterface obj;t,它可以吞下从它继承的所有类对象,因为我的示例代码运行良好但在我需要实现函数我不需要因为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();
        }
    }
c# inheritance interface virtual
1个回答
3
投票

在这里阅读:Interface Segregation Principle

我想你需要看一下qazxsw poi,这是qazxsw poi

这个原则说如果你在接口中有方法,那么创建单独的拦截,你的类不会实现。

我建议像这样设计

SOLID
© www.soinside.com 2019 - 2024. All rights reserved.