抽象类VS接口 [英] Abstract classes vs Interfaces

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

问题描述

我对抽象类在C#中的使用有点糊涂。在C ++中,它是有道理的定义哪些类继承抽象类可以遵循的模板。但是,在C#不界面达到同样的目的?

I'm a bit confused about the usage of Abstract classes in C#. In C++, it makes sense to define a template which classes inheriting the abstract class can follow. But, in C# doesn't Interface serve the same purpose?

真正的抽象类可以有它不受接口提供的默认实现。所以,如果实现不需要被包含在基类,是它更好地去为接口?

True that abstract classes can have default implementation which is not provided by Interfaces. So if implementation doesn't need to be included in base class, is it better to go for Interfaces?

推荐答案

我还是喜欢提供一个默认的抽象实现一个接口,假设这是一个实质性的接口(以及它是有道理的)。你永远不知道什么时候会添加一些有可能被纳入,并给予免费的人谁从抽象基类继承一个简单的默认实现的接口。

I still like to provide a default abstract implementation of an interface, assuming it's a substantial interface (and it makes sense). You never know when you might add something to the interface that has an easy default implementation that could be included and given "for free" to anyone who inherits from the abstract base class.

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

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