我在论坛和博客文章中看到了以下的代码布局,并进行了改编以便提出一些问题。
public interface IService<T>
{
int Add(T entity);
void Update(T entity);
}
public abstract class ServiceBase<T> : IService<T>
{
public int Add(T entity) { ... }
public void Update(T entity) { ... }
}
public interface ICarService : IService<Car>
{
}
public class SomeBaseClass : ServiceBase<Car>, ICarService
{
public int Add(Car entity);
public void Update(Car entity);
}
我不明白的是抽象类实现接口的好处。对我来说,这只是有点重复,我无法理解抽象类实现接口的好处。
- 为什么抽象类
ServiceBase<T>
不能直接定义而需要继承IService
接口?这是在重复代码吗? - 为什么
SomeBaseClass
也需要实现ICarService
接口呢?难道ServiceBase不足以满足要求吗?