解释学生的界面 [英] Explaining Interfaces to Students

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

问题描述

几年来我一直是编程模块介绍的助教 - 一年级本科生的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()的方法,我们会需要为我们希望画家能够绘制的每种类型的对象都有一个新的集合

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