如何在不必实现接口方法的情况下使用它们?

How are interface methods able to be utilised without having to implement them?

这是一个基本的 Java 接口问题,但在 servlet 的上下文中。

我理解接口是完全抽象的,因为它们的 none 方法可以有不同于抽象 类 的实现。 HttpServletRequest 是一个接口。创建 servlet 时,doPost() 和 doGet() 方法通过代码使用此接口:

doPost(HttpServletRequest request, HttpServletResponse response){ // implementation }

我的问题是:

What are request and response? Are they objects?

它们只是引用,指向一些对象,就像任何其他引用一样。这里没什么特别的。

how do the methods of ‘request’ get implemented as seen in the code.

HttpServletRequest 只是一个 API,其实现由您正在使用的 servlet 容器提供(可能是 tomcat)。在内部,对于每个请求,容器都会创建 HttpServletRequestHttpServletResponse 实现的对象,并将其传递给您的 doPostdoGet 方法。

您获得了 class 对象的引用,它实际实现了 HttpServletRequest

现在您如何获取该对象,网络服务器会初始化这些对象并通过将这些引用作为参数传递来调用您的 doGetdoPost 方法。