使用接口有什么好处 [英] What is the advantage of using interfaces

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

问题描述

假设你有一个界面

public interface Change {

    void updateUser();
    void deleteUser();
    void helpUser();

}

我读过接口是Java实现多个接口的方式遗产。您实现了一个接口,然后您可以访问其方法。我不明白的是,这些方法在界面中没有任何主体,所以你需要在课堂上给它们一个主体。因此,如果您的接口由多个类实现,则需要在多个类中为该方法提供一个主体。为什么这比在你的类中使用单独的方法更好,而不是实现接口?

I've read that interfaces are Java's way to implement multiple inheritance. You implement an interface, then you have access to its methods. What I don't understand is, the methods don't have any body in the interface, so you need to give them a body in your class. So if you an interface is implemented by more than one class, you need to give the method a body in more than one class. Why is this better than just having individual methods in your classes, and not implementing an interface?

推荐答案

我在大学的教授曾经给过描述多态性和封装的伟大轶事。它是这样的。

My professor in college once gave a great anecdote to describe polymorphism and encapsulation. It went like this.

这里有人知道汽水机的工作原理吗? (提示混淆了我们为什么甚至谈论这个问题的一瞥。)不是吗?让我告诉你。

Does anyone here know how a soda machine works? (Cue confused glances about why we'd even talk about this.) No? Let me tell you.

你放弃你的改变,在机器内部是一只小猴子,他会计算你所有的变化,以确保你投入足够的钱。当您按下苏打水的按钮时,会发出一点点光线告诉猴子您按下了哪个按钮,如果您输入了正确的更改,他会抓住您的选择并将其扔进小洞中以便您拿起苏打水。

You drop in your change, and inside the machine is a little monkey who counts all your change to make sure you put in enough money. When you press the button for your soda, a little light comes on telling the monkey which button you pressed, and if you entered the right amount of change, he grabs your choice and throws it into the little hole for you to grab your soda.

这是封装的概念。我们隐藏了汽水机的实施。除非它有一个那些花哨,清晰的窗户让你看到内部,你真的不知道它是如何工作的。所有你知道的是你收到一些现金,你按下一个按钮,如果你放入足够的,你得到你的饮料。

This is the concept of encapsulation. We hide the implementation of the soda machine. Unless it's got one of those fancy, clear windows to let you see the inside, you honestly have no idea how it really works. All you know is that you put in some cash, you press a button, and if you put in enough, you get your drink.

要添加,你知道如何使用汽水机的界面,因此只要机器的界面遵循通常的汽水机界面,您就可以使用它。这称为接口合约。这台机器可以将来自南极洲的饮料带到传送带上,只要你拿到饮料,感冒就可以换回来。

To add to that, you know how to use a soda machine's interface, so therefore as long as the machine's interface follows the usual soda machine interface, you can use it. This is called the interface contract. The machine can be bringing the drinks from Antarctica on a conveyor belt for all you care, as long as you get your drink, it's cold, and you get change back.

多态性是这样的想法,当你使用苏打水机界面时,它可能会做不同的事情。这就是封装和多态性密切相关的原因。在多态性方面,你所知道的就是你正在使用一个 SodaMachine 实现,它可以被改变,因此,可以在幕后完成不同的事情。这导致了多态性的驱动概念,即一个对象 SodaMachine 的实际作为 MonkeySodaMachine 和一个 ConveyorSodaMachine 取决于接口背后的机器。

Polymorphism is the idea that when you use the soda machine interface, it could be doing different things. This is why encapsulation and polymorphism are closely related. In polymorphism, all you know is that you're using a SodaMachine implementation, which can be changed, and as a result, different things can be done behind the scenes. This leads to the driving concept of polymorphism, which is the ability of one object, the SodaMachine, to actually act as both a MonkeySodaMachine and a ConveyorSodaMachine depending on the machine actually behind the interface.

可能不是逐字逐句,但足够接近。从本质上讲,它归结为两个概念: polymorphism 封装。如果您想要澄清,请告诉我。

Probably not word-for-word, but close enough. Essentially it boils down to two concepts: polymorphism and encapsulation. Let me know if you want clarification.

这篇关于使用接口有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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