向学生解释接口 [英] Explaining Interfaces to Students

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

问题描述

几年来,我是一名助教,为本科一年级学生介绍编程模块 - Java.

For a few years I was a teaching assistant for an introduction to programming module - Java for first year undergraduates.

大部分情况下进展顺利,我们设法将面向对象编程很好地传达给学生,但学生很少注意到的一件事是接口.

Mostly it went well and we managed to get object-oriented programming across to the students quite well, but one thing that students rarely saw the point of was interfaces.

我们给出的几乎所有解释要么过于人为,对学习没有用处,要么与他们作为初学者的立场相去甚远.我们往往得到的反应是我……明白了",翻译为我不明白,听起来也没什么用".

Pretty much any explanation we gave either came across as too contrived to be useful for learning, or too far removed from their position as beginners. The reaction we tended to get was "I... see," translated as "I don't understand and they don't sound useful".

这里有没有人有办法成功地教学生有关界面的知识?我不再是助教了,但它总是对我唠叨.

Anyone here have a way of successfully teaching students about interfaces? I'm not a teaching assistant any more, but it's always nagged at me.

推荐答案

如果你想向初学者解释它,我会坚持接口可以促进代码重用和代码模块化的想法:

If you are trying to explain it to beginners I would stick with the idea that interfaces can promote code reuse and modularity within the code:

例如,假设我们要绘制一些对象:

For example lets say we are going to paint some objects:

public class Painter {
    private List<Paintable> paintableObjects;

    public Painter(){
       paintableObjects = new ArrayList<Paintable>();
    }

    public void paintAllObjects(){
        for(Paintable paintable : paintableObjects){
            paintable.paint();
        }
    }
}

public interface Paintable {
     public void paint();
}

现在你可以向学生解释,如果没有 Paintable 接口,Painter 对象需要有方法来绘制某些类型的对象,比如名为 paintFences()paintRocks() 的方法 并且我们需要为我们希望画家能够绘制的每种类型的对象都有一个新的 Collection.

Now you could explain to the students that without Paintable interface the Painter object would need to have methods to paint certain types of objects, like a method called paintFences() and paintRocks() and we would need to have a new Collection for each type of objects we want the painter to be able to paint.

但幸运的是,我们有接口可以让绘制对象变得轻而易举,并且对象的绘制方式完全取决于实现 Paintable 接口的类.

But thankfully we have interfaces which make painting objects a breeze and how objects are painted is left entirely up to classes that implement the Paintable interface.

编辑

我忘记提及的另一个好处是,如果您需要将新对象添加到代码库中进行绘制,您需要做的就是创建一个实现 Paintable 的新类,而 Painter 类永远不必更改.从这个意义上说,Painter 类从不依赖于它要绘制的对象,它只需要能够绘制它们.

Another benefit that I forgot to mention is that if you ever need to add new object to paint to your code base, all you need to do is create a new class that implements Paintable and the Painter class never has to change. In this sense the Painter class is never dependent upon the objects it is going to paint, it only needs to be able to paint them.

编辑 2

James Raybould 让我想起了我忘记提及的接口的一个关键用途:在组件之间拥有一个接口,例如 Paintable 对象和 Painter 对象,可以让您更轻松地与其他人一起开发.一个开发人员可以在 Painter 对象上工作,另一个可以在 Paintable 对象上工作,他们需要做的就是预先定义一个他们都将使用的公共接口.我知道当我在大学级别的项目中与其他人一起工作时,当你试图让每个人都在项目的不同部分工作并且最终仍然让所有组件很好地结合在一起时,它真的很有帮助.

James Raybould reminded me of a key use of interfaces I forgot to mention: Having an interface between your components, like the Paintable objects and Painter objects, allows you to more easily develop with other people. One developer can work on the Painter objects and another can work on the Paintable objects and all they have to do to function properly together is define a common interface beforehand that they will both use. I know when I've worked on projects with other people in college level projects its really helpful when you are trying to have everyone work on different parts of the project and still have all components come together nicely in the end.

这篇关于向学生解释接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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