界面与构图 [英] interface vs composition

查看:97
本文介绍了界面与构图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我理解界面和抽象之间的区别。 Abstract设置默认行为,在纯抽象的情况下,行为需要由派生类设置。接口是您需要的,无需基类的开销。那么界面优于作曲的优势是什么?我能想到的唯一优势是在基类中使用受保护的字段。我缺少什么?

I think I understand the difference between interface and abstract. Abstract sets default behavior and in cases of pure abstract, behavior needs to be set by derived class. Interface is a take what you need without the overhead from a base class. So what is the advantage of interface over composition? The only advantage I can think is use of protected fields in the base class. What am I missing?

推荐答案

界面定义了你的使用方式。

An interface defines how you will be used.

您继承以便重复使用。这意味着您希望适应某种框架。如果您不需要适应框架,即使是您自己制作的框架,也不要继承。

You inherit in order to be reused. This means you want to fit into some framework. If you don't need to fit into a framework, even one of your own making, don't inherit.

组合是一个实现细节。为了获得基类的实现,不要继承,编写它。只有在允许你适应框架时才继承。

Composition is an implementation detail. Don't inherit in order to get the implementation of the base class, compose it. Only inherit if it allows you to fit into a framework.

这篇关于界面与构图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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