我们为什么要实现接口? [英] Why we are implementing interfaces ?

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

问题描述

我们为什么要实施,例如 ICloneable 或 IDisposable.我不是在问 ICloneable 或 IDisposable 做什么,但我想了解什么是实现这些接口的好理由,而不是仅仅编写一个处理或克隆我们的对象的方法?

Why are we implementing, for example ICloneable or IDisposable. I'm not asking what ICloneable or IDisposable do, but I want to learn what's a good reason to implement these interfaces rather than just writing a method which disposes or clones our objects?

推荐答案

使用接口可以使这些功能的使用保持一致.这样,当另一个类想要/需要使用您的类时,它可以将其用作可克隆的一次性对象,而无需担心您的特定实现细节.

Using interfaces keeps the use of those pieces of functionality consistent. That way, when another class wants to / needs to use your class, it can act on it as a cloneable, disposable object without worrying about your particular implementation details.

这篇关于我们为什么要实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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