c#抽象类实现接口 [英] c# Abstract Class implementing an Interface

查看:50
本文介绍了c#抽象类实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读论坛和其他博客文章中看到了以下代码布局并进行了改编,以便提出一些问题.

I've seen the following code layout reading forums and other blog posts and adapted in order to ask a few questions.

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);
}

我不明白让抽象类实现接口的好处.对我来说只是感觉有点重复,我无法理解使用抽象类实现接口的好处.

What I don't understand is the benefit of having the abstract class implmenting the interface. To me it just feels a bit repetitive and I cannot understand the benefit of having an abstract class implementing the interface.

  1. 为什么抽象类ServiceBase<T>不直接定义而不需要继承IService接口?这是把代码加倍了吗?
  2. 为什么SomeBaseClass 还必须实现ICarService?难道 ServiceBase 不够用吗?
  1. Why doesn't the abstract class ServiceBase<T> just define as is without the need to inherit the IService interface? Is this doubling up the code?
  2. Why must the SomeBaseClass also implment the ICarService? Shouldn't the ServiceBase be sufficient?

推荐答案

广告 1:额外的抽象基类允许您在不破坏实现的情况下改进接口.假设没有抽象基类,并且您要扩展接口,比如说添加一个新方法.然后你的实现被破坏了,因为你的类不再实现接口.

Ad 1: The additional abstract base class allows you to evolve the interface without breaking the implementation. Supposed there was no abstract base class, and you'd extend the interface, let's say by adding a new method. Then your implementation was broken, because your class does not implement the interface any longer.

使用一个额外的抽象基类,你可以把它分开:如果你在接口中添加一个新方法,你可以在基类中提供一个虚拟实现,你的所有子类都可以保持不变,并且可以被采用稍后再匹配新界面.

Using an additional abstract base class you can separate this: If you add a new method to the interface, you can provide a virtual implementation in the base class and all your sub-classes can stay the same, and can be adopted to match the new interface at a later point in time.

此外,这种组合允许您定义契约(使用接口)并提供一些默认机制(使用抽象基类).任何对默认值满意的人都可以从抽象基类继承.任何想要对任何小细节进行超级精细控制的人都可以手动实现该界面.

Moreover, this combination allows you to define a contract (using the interface) and provide some default mechanisms (using the abstract base class). Anyone who's fine with the defaults can inherit from the abstract base class. Anyone who wants super-duper fine control about any little detail can implement the interface manually.

广告 2:从技术角度来看,没有需要在最终类中实现接口.但是,这再次允许您将事物彼此分开发展.CarService 肯定是 Service,但也许更多.也许只有一个 CarService 需要一些额外的东西,这些东西不应该进入公共接口,也不应该进入服务基类.

Ad 2: From a technical point of view there is no NEED to implement the interface in the final class. But this, again, allows you to evolve things separately from each other. A CarService is for sure a Service<Car>, but maybe it's even more. Maybe only a CarService needs some additional stuff that should not go into the common interface nor into the service base class.

我想这就是为什么;-)

I guess that's why ;-)

这篇关于c#抽象类实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆