C#接口和基类

时间:2012-01-18 15:51:28

标签: c# oop inheritance

我有一个C#接口,以及一个实现该接口的具体类。我现在想要创建另一个实现该接口的类。很简单。

但是,大多数方法在类中都是完全相同的,只有几种方法实际上会发生变化。

我不想复制第一个包含在我的第二个类中的所有逻辑。

如何创建第二个类,并在我的第一个类中使用逻辑,除了额外的东西?

我的界面名为IEventRepository,我的第一个类叫做BaseEvents。我现在想创建一个名为FooBarEvents的新类。

我对FooBarEvents的类定义是:

public class FooBarEvents : BaseEvents, IEventRepository

我的意图是在每个重复代码的方法中使用return base.Method()。

我认为这不正确?

2 个答案:

答案 0 :(得分:3)

以下代码显示了如何使用抽象基类提供某些接口方法的通用实现,并为其他人提供自定义实现。

public interface IEventRepository
{
  void Method1();
  void Method2();
}

public abstract class BaseEvents : IEventRepository
{
  public void Method1() 
  {
    Console.WriteLine("This is shared functionality");
  }

  public abstract void Method2();
}

public class Implementation1 : BaseEvents
{
  override public void Method2()
  {
    Console.WriteLine("Impl1.Method2");
  }
}

public class Implementation2 : BaseEvents
{
  override public void Method2()
  {
    Console.WriteLine("Impl2.Method2");
  }
}

public class Program
{
  static void Main(string[] args)
  {
    var implementations = new List<IEventRepository> { new Implementation1(), new Implementation2() };

    foreach (var i in implementations) 
    {
       Console.WriteLine(i.GetType().Name);
       Console.Write("\t");
       i.Method1();  // writes 'This is shared functionality'

       Console.Write("\t");
       i.Method2(); // writes type specific message
    }
  }

}

答案 1 :(得分:0)

有几种不同的方法。

一。完全跳过界面,并使其成为一个抽象类。这在工作时更简单,但事实上你只能有一个基类限制在C#中使用

public abstract class EventRepository
{
  public abstract int MustBeOverridden(string str);//classes have to override this
  public virtual int CanBeOverridden(int i)//classes can override but may choose not to.
  {
    return 4;
  }
  public int CannotOverride(string str)//this is always the same
  {
    return MustBeOverridden(str) + 3;//can make use of this
  }
}

您可以让一个类实现该接口,另一个类从它派生:

public interface IEventRepository
{
  int Method1(string str);
  int Method2(string str);
}

public class EventClass1 : IEventRepository
{
  public int Method1(string str)//can't be overridden as not marked virtual
  {
    return 1;
  }
  public virtual int Method2(string str)//can be overridden
  {
    return 2;
  }
}

public class EventClass2 : EventClass1
{
  public override int Method2(string str)
  {
    return -2;
  }
}

让他们都覆盖一个给出一些常见行为的抽象类:

public abstract class EventClass : IEventRepository
{
  public abstract int Method1(string str);
  public int Method2(string str)
  {
    return 2;
  }
}

public class EventClass1 : EventClass
{
  public override int Method1(string str)
  {
    return 1;
  }
}
public class EventClass2 : EventClass
{
  public override int Method1(string str)
  {
    return -1;
  }
}

他们也可能使用与层次结构无关的静态助手类,但它确实提供了在实现功能时有用的方法。

虽然这种模式很谨慎:

public class EventClass1 : IEventRepository
{
  public int Method1(string str)//not override-able
  {
    return 1;
  }
  public int Method2(string str)//not override-able
  {
    return 2;
  }
}
public class EventClass2 : EventClass1, IEventRepository
{
  //We really want our own Method1!
  public new int Method1(string str)
  {
    return 3;
  }
  int IEventRepository.Method1(string str)
  {
    return -1;
  }
}

EventClass2 e2 = new EventClass2();
EventClass1 e1 = e2;
IEventRepository ie = e2;
Console.WriteLine(e2.Method1(null));//3
Console.WriteLine(e1.Method1(null));//1
Console.WriteLine(ie.Method1(null));//-1

即使更明智地定义IEventRepository.Method1,上述情况也可能导致混淆。