为什么接口或抽象类有用?(或者为了什么?) [英] Why is an interface or an abstract class useful? (or for what?)

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

问题描述

所以我的问题是,为什么要使用接口或抽象类?它们为什么有用,有什么用?

So my question is, why to use interfaces or abstract classes? Why are they useful, and for what?

我可以在哪里智能地使用它们?

Where can i use them intelligently?

推荐答案

接口允许您表达类型的功能,而无需担心它是如何完成的.可以在不影响客户的情况下随意更改实施方式.

Interfaces allow you to express what a type does without worrying about how it is done. Implementation can be changed at will without impacting clients.

抽象类就像接口,只是它们允许您为存在的方法提供合理的默认行为.

Abstract classes are like interfaces except they allow you to provide sensible default behavior for methods where it exists.

用法和示例取决于语言.如果您了解 Java,则可以在整个 API 中找到接口和抽象类的示例.java.util 集合有很多.

Use and examples depend on the language. If you know Java, you can find examples of both interfaces and abstract classes throughout the API. The java.util collections have plenty of both.

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

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