如何解释接口和抽象类与非程序员之间的区别? [英] How can you explain the difference between an interface and an abstract class to a non-programmer?

查看:95
本文介绍了如何解释接口和抽象类与非程序员之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

何时使用接口而不是抽象类,反之亦然?

我正在向非程序员教授OOP概念。我想知道如何解释界面和抽象类之间的区别。

我实际上正在寻找的是一个真实世界的例子,可以帮助突出差异两者之间。

Hi, I am teaching OOP concepts to non-programmers. I wanted to know how can you explain the difference between an interface and an abstract class.
What I am actually looking for, is a real world example that can help highlight the difference between the two.

推荐答案

播放器界面



在我的Java中课程我经常使用这个那种形象并问:这是什么?

The player Interface

In my Java courses I often use this kind of image and ask: "What is this ?"

每次有人会说那是一名球员。从这张图片中你可以教任何人界面是什么。此接口允许任何用户播放某些内容。每个人都知道这些按钮的含义,即使你不知道将要做什么,你可以使用这个界面的任何东西,你知道小箭头将播放,其他箭头可能会向前或向后发送你。
所有具有这些按钮的东西都将提供任何用户在开始使用之前都会知道的标准行为。

Every time someone will say "that's a player". From this image you can teach anybody what an interface is. This Interface allow any user to "play" something. Everybody knows what these buttons mean, even if you don't know what exactly will be done, you can use anything with this interface and you know that the little arrow will "play" and other arrows will probably send you forward or backward. Everything that will have those buttons will provide a standard behavior that any user will know before even starting to use them.

我通常会尽量避免合约 可以被误解的词。

I usually try to avoid the "contract" word which can be misunderstood.

然后从播放界面,我转到录像机(或DVD) )球员。 DVD播放器的每个构造函数都必须添加一些特殊功能,以将简单的未知播放器转换为DVD播放器。例如弹出按钮。他们必须正确实现播放器。

播放按钮将启动DVD的内容。

And then from the Play Interface, I go to the VCR (or DVD) player. Every constructor of DVD player must add some special functions to transform a simple unknown player into a DVD player. For example the eject button. And they must correctly implement Player.
The play button will launch the content of the DVD.

但即使DVD播放器提供的基本行为也是如此。一台DVD播放机,并非一切都已完成。你不能简单地拥有一个DVD播放器,它有一个品牌,大多数时候它有自己的固件。这次你需要扩展DVD播放器抽象类来添加你自己的小组件。

But even if DVD Player provide the basic behavior of a DVD player, not everything is done. You can't simply have "a" DVD player, it has a brand and most of the time it has its own firmware. A this time you'll need to extend the DVD Player abstract class to add your own little components.

这篇关于如何解释接口和抽象类与非程序员之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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