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

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

问题描述

我已经看到了下面的代码布局读书论坛等博客文章和适应,以问几个问题。

 公共接口IService< T> 
{
INT加入(T实体);
无效更新(T实体);
}

公共抽象类ServiceBase< T> :IService< T>
{
公众诠释添加(T实体){...}
公共无效更新(T实体){...}
}

公共接口ICarService:IService<汽车>
{
}

公共类SomeBaseClass:ServiceBase<汽车>中ICarService
{
公众诠释添加(车载实体);
公共无效更新(车载实体);
}



我不明白的是具有抽象类implmenting的受益接口。对我来说,只是感觉有点重复,我无法理解有一个抽象类实现接口的好处。




  1. 为什么没有抽象类 ServiceBase< T> 仅仅定义为是无需继承IService接口?难道这加倍的代码?

  2. 为什么一定要在 SomeBaseClass 也implment的 ICarService ?如果不是ServiceBase足够


解决方案

广告1:额外的抽象基类允许你进化界面不破的实现。应该没有抽象基类,你会扩展接口,让我们加入了新的方法说。然后你的实现被打破了,因为你的类未实现该接口不再



使用一个额外的抽象基类可以分出这一点:如果添加一个新的方法该接口,可以在基类中提供了一个虚拟的实现和所有的子类可以保持不变,并可以通过匹配在以后某个时间点的新界面。



此外,这样的组合可以让你定义一个合同(使用接口),并提供了一​​些默认的机制(使用抽象基类)。任何人谁是与违约罚款可以从抽象基类继承。 。谁想要的任何小细节可以手动实现接口超级骗子精控



广告2:从技术角度看,没有的需要的实施在最后类的接口。但是,这,再次,允许你从彼此独立发展的事情。 A CarService 是肯定的一个服务与LT;汽车> ,但也许它甚至更多。也许只有一个 CarService 需要一些额外的东西,不应该进入通用接口和到服务的基类。



我想这就是为什么; - )


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. 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?

解决方案

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.

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天全站免登陆