返回的接口对象如何声明其方法 [英] How do objects of interfaces being returned have their methods declared

查看:134
本文介绍了返回的接口对象如何声明其方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这感觉就像一个愚蠢的问题,但它使我更加恶化,因为我不明白方法的声明在哪里。在我的代码中我可以做类似的事情(显然这是一个伪代码)

This feels like a silly question, but it aggravates me as I don't understand where the methods are declared. In my code I can do things like (obviously this is a bit pseudo code)

ResultSet rs = DB.getConnection.sendSQL(select * from [table])

我知道发送SQL会返回'结果集'虽然'ResultSet'只是一个接口,但返回的对象实现了该接口。

I understand that the sending of the SQL returns a 'result set' and although a 'ResultSet' is only an interface the returned object implements that interface.

我的问题是返回的对象如何实现该接口?从我读到的界面只能定义一个方法签名,而不是一个完整的函数。

My question is how does the returned object implement that interface? From what I read interface can only define a method signature, not a complete function.

那么函数定义在哪里?显然不在ResultSet界面中!我是否正确理解这是数据库驱动程序的工作? (我想我可能刚回答了我自己的问题)。

So where are the functions defined? Obviously not in the ResultSet interface! Am I correct in understanding that this the job of the database Driver? (I think I may have just answered my own question).

我可以用一个功能齐全的方法编写一个接口吗?那么任何实现类是否会自动实现该方法,或者我只是通过调用'super'或其他东西来覆盖它?

Can I write an interface with a fully functioning method? Will any implementing classes then automatically implement that method, or do I just overwrite it with a call to 'super' or something?

我刚刚在SO $上找到了另一个问题b $ b 抽象如何像Connection,Statement等预定义接口的方法执行一些没有正文的任务?
这是一个完美的复制,但很多更好的制定。并且一个很好的答案

I just found this other question on SO How does abstract method of predefined interface like Connection, Statement etc. perform some task without having body? which is a perfect duplicate, but much bette formulated. And a great answer

推荐答案

实现接口的类负责声明接口中的所有方法,并在适当的位置实现这些方法的内部逻辑。

Classes that implement an interface are responsible for declaring all of the methods in the interface, and where appropriate implementing the internal logic for those methods.

接口本质上是说你必须为此提供功能,但我不在乎如何你这样做。你只需要输入这些类型并提供这种类型的输出。您可以在该课程中实现从输入到输出的方式。

The interface is essentially saying "You have to provide functionality for this, but I don't care how you do that. You just have to take inputs of these types and provide output of this type." The class is where you implement how you get from the inputs to the outputs.

这篇关于返回的接口对象如何声明其方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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