How to call an explicitly implemented interface-method on the base class

后端 未结 7 1199
死守一世寂寞
死守一世寂寞 2020-12-05 09:56

I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly:

interface I
{
  int M();
}

class A : I
{
          


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 10:40

    On option would be to create a base class that doesn't excplicitly implement an interface, and to only implement the interface on the subclass.

    public class MyBase
    {
        public void MethodA()
        {
            //Do things
        }
    }
    
    public interface IMyInterface
    {
        void MethodA();
        void MethodB();
    }
    
    
    public class MySub: MyBase, IMyInterface
    {
        public void MethodB()
        {
            //Do things
        }
    }
    

    Doing this will allow you to access the base classes MethodA without having to implemnet it in MySyb.

提交回复
热议问题