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

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

问题描述

这可能是一个通用的 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.

什么时候需要使用接口,什么时候需要使用抽象类?

推荐答案

我写了一篇关于这个的文章:

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