There are several different approaches.
One. Skip the interface completely and make it an abstract class. It is easier when it works, but the fact that you can only use one base class for use in C #
public abstract class EventRepository { public abstract int MustBeOverridden(string str);
You can have one class that implements the interface, and the other from it:
public interface IEventRepository { int Method1(string str); int Method2(string str); } public class EventClass1 : IEventRepository { public int Method1(string str)
Ask them to override the abstract class, which gives some general behavior:
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; } }
They can also use a static helper class that has nothing to do with hierarchy, but which provides methods that are useful in implementing functionality.
Be careful though from this template:
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
Even when IEventRepository.Method1
defined more reasonably, the above can lead to confusion.
Jon hanna
source share