为什么不使用抽象类而不是接口? [英] Reasons not to use abstract class instead of interface?

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

问题描述

我正在考虑对所有抽象成员使用抽象类而不是接口,以避免显式接口实现样板代码.因此,而不是

I'm considering using an abstract class with all abstract members instead of an interface in order to avoid explicit interface implementation boiler-plate code. So instead of

type IMyInterface =
    abstract Name : string
    abstract Text : string

type MyClass() =
    member __.Name = "name"
    member __.Text = "text"
    interface IMyInterface with
        member this.Name = this.Name
        member this.Text = this.Text

我会

[<AbstractClass>]
type MyAbstractClass() =
    abstract Name : string
    abstract Text : string

type MyClass() = 
    inherit MyAbstractClass()
    override __.Name = "name"
    override __.Text = "text"

在这里我应该注意的任何警告或暗示的字眼?

Any words of caution or implications I should be aware of here?

推荐答案

您唯一需要注意并有意识地做出决定的事情是,一个类只能从一个类继承而实现许多接口.

Only thing that you should be aware and make a conscious decision is a class can inherit from only one class but implement many interfaces.

除此之外,有关使用Abstract类或接口的一些建议:

Apart from that, some recommendations on using Abstract classes or Interfaces:

  • 如果您希望创建组件的多个版本,请创建一个 抽象类.抽象类 提供一种简单易行的方法 版本化您的组件.通过更新 基类,所有继承的类 会自动更新为 改变.另一方面,接口 创建后无法更改.如果一个 接口的新版本是 必需,您必须创建一个全新的 界面.
  • 如果您要创建的功能将在广泛的范围内有用 范围广泛的不同对象,请使用 界面.抽象类应该是 主要用于 紧密相关,而接口 最适合提供普通 无关类的功能.
  • 如果您要设计小而简洁的功能,请使用 接口.如果您正在设计大型 功能单元,使用摘要 课.
  • 如果您要在所有人之间提供通用的,已实现的功能 组件的实现,使用 抽象类.抽象类 允许您部分实施您的 类,而接口不包含 实施任何成员.
  • If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  • If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
  • If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

http://msdn.microsoft.com/zh-cn/library/scsyfw1d%28vs.71%29.aspx

我个人认为这些建议是正确的.另一方面,尤其是接口,一旦创建就无法更改.如果需要新版本的界面,则必须创建一个全新的界面.是非常重要的一点.

Personally, I feel these recommendations are spot on. Especially Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. is a very important point.

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

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