为什么我们需要接口,如果它只声明方法签名呢?

huangapple go评论67阅读模式
英文:

Why do we need interfaces if it only declares the method signature?

问题

所有在接口中声明的方法都是抽象的,我们必须在实现接口的类中重新编写具有签名和主体的方法。那么使用接口的目的是什么?

英文:

All the methods declared in interfaces are abstract and we have to re-write the method with signature and body in the class that implements the interface. So what's the point of using the interface?

答案1

得分: 4

一般而言,接口被视为开发者之间的契约。我的意思是,假设你正在为公共使用开发专有的 API。现在,你不希望每个人都能直接看到你实现某些内容的源代码,因为那就是你的整个产品。相反,你会暴露一个接口,展示给其他开发者期望的参数类型和返回值,同时保证你的代码将实现其被用于的目的,而无需透露其工作原理。

这也为代码的可扩展性创造了可能,因为通过允许某个方法接受一个接口,而不是特定的实现,你允许实现可以被更改或者可能得到改进,只要它仍然实现相同的接口,从而遵循相同的保证。

定义一个接口就像在说:“我希望能够调用这些函数,它们接受这些参数并返回这些值,但我不关心你是如何实现它的”。

英文:

Generally interfaces are considered to be contracts between developers. By this I mean, let's say you are developing a proprietary API for public use. Now you don't want everyone to be able to directly see your source code of how you implemented something, because that is your whole product. What you do instead is expose an interface which shows other developers what parameter types and return values to expect, and guarantees that your code will accomplish the purpose that it is being used for, without having to reveal how it works.

This also allows for the extensibility of code, because by allowing a certain method to accept an interface, rather than a specific implementation, you then allow the implementation to be changed or possibly improved, as long as it still implements the same interface and therefore adheres to the same guarantees.

Defining an interface is like saying "I expect to be able to have these functions that I can call that accept these parameters and return these values, but I don't care how you do it"

答案2

得分: 0

接口就像其名称所示,为某物提供了接口。假设我想要一个应用程序,该应用程序从用户处接收输入并将其存储在数据库中,然后在请求时从数据库中提取数据以供用户显示。我可以简单地拥有一个声明了存储和提取数据库数据方法的接口。因此,接收输入并显示数据的用户表单可以使用这些方法。
这使我能够在决定更改数据库时灵活地更改实现。我不需要更改使用接口中声明的方法的部分,因为所有的实现都会有这些方法。

我认为这个文档(https://docs.oracle.com/javase/tutorial/java/concepts/interface.html)也会有所帮助。

我认为我还可以在这里补充一点,假设我想要使用Facebook(或者其他一些应用)提供的接口。我可以浏览所提供方法的列表,以查看哪个方法适用于我。这比不得不浏览所有实现细节要简单。这就是我认为@Tarun在上面的回答中也提到的内容。

英文:

Interface just like the name suggest provides interface to something. So let's say I want an application that takes input from user and stores it in a database and later on fetches data from database on request to display from user. I can simply have an interface that declares methods to store and fetch from the database. So the user form that takes input and displays data can use these methods.
This gives me flexibility to change the implementation if I decide to change the database. And I would not have to change the part using the methods declared in the interface. Since all implementation will have those methods.

I think this doc(https://docs.oracle.com/javase/tutorial/java/concepts/interface.html) also will be helpful.

I think I can add one more thing here, let's say I want to use the interface provided by facebook(or some other app if you like). I can go through the list of methods provided to see which one works for me. It makes things simpler than having to go through all the implementation details. That's what I think @Tarun also said in above answer.

huangapple
  • 本文由 发表于 2020年4月9日 13:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61114782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定