何时使用接口而不是抽象类,反之亦然? [英] When to use an interface instead of an abstract class and vice versa?

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

问题描述

这可能是一个通用的OOP问题。我想根据它们的用法在接口和抽象类之间进行一般性比较。

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage.

什么时候想要使用界面?何时想要使用抽象类

When would one want to use an interface and when would one want to use an abstract class?

推荐答案

我写了一篇关于此的文章:

I wrote an article about that:

抽象类和接口

总结:

当我们谈论抽象类时,我们定义了对象类型的特征;指定 对象是什么

When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is.

当我们谈论接口并定义我们承诺提供的功能时,我们正在谈论建立一个关于 对象可以做什么的合同。

When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object can do.

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

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