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

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

问题描述

可能的重复:
何时改用接口抽象类,反之亦然?

我正在向非程序员教授 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.

然后从播放界面转到 VCR(或 DVD)播放器.每个 DVD 播放器的构造函数都必须添加一些特殊的函数,才能将一个简单的未知播放器变成一个 DVD 播放器.例如弹出按钮.并且他们必须正确实现 Player.
播放按钮将启动 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 播放器的基本行为,也并非一切都已完成.你不能简单地拥有a"DVD 播放器,它有一个品牌,大多数时候它有自己的固件.A 这次您需要扩展 DVD Player 抽象类以添加您自己的小组件.

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