help with understanding interface-declaration of an object
could someone please explain this java code for me. i find it really hard to understand.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = request.getParameter("firstName").toString();
process开发者_开发问答Request(request, response);
}
so request is an object of the interface class HttpServletRequest which got some empty methods.
but how can it return any data if the method getParameter() is empty.
and also, i cant find getParameter() in the HttpServletRequest interface. But shouldn´t it be one, cause request is declared as a HttpServletRequest and thus own all its methods and fields?
and which class is toString() coming from.
So HttpServletRequest is an interface. Interfaces declare methods for actual classes to implement. In this case, your servlet container will provide the implementation for you (depends what you are using: Tomcat, Jetty, etc). Remember that interfaces can also extend each other. In this case HttpServletRequest extends ServletRequest. You should look at ServletRequest for additional methods.
Just to be clear, the methods of HttpSerlvetRequest aren't "empty". Have been implemented by the container, so when the container calls your method above, the objects it is passing to you are full-fledged objects that happen to implement the HttpServletRequest interface. this is why you can cast them as such.
Edit: toString() method comes from the java.lang.Object class. This is the base class for all java objects.
@noname, @darren did a good job answering your question, but your question to him suggests that you need even more fundamental matters explained. So...
request
is an object of a class that implements HttpServletRequest
. And when we look to the declaration of HttpServletRequest we see:
public interface HttpServletRequest extends ServletRequest {
Saying that a class implements
an interface, or that one interface extends
another, can usefully be expressed in more human terms as IS A. So request
is an object of a class that IS A HttpServletRequest
, which IS A ServletRequest
. Boiled all the way down:
request
IS A ServletRequest
.
This means that any methods ServletRequest
declares can be invoked for your request
object, and the declaration of ServletRequest includes:
public String getParameter(String name);
And that's why we can call request.getParameter(someString)
.
You say:
which class is it that has them as methods? i have to know this information to be able to code and know how to think.
But that's not right. You don't need to know which specific class holds the implementation of the method, and you will come to understand this as a strength. It is an abstraction, and abstraction is at the heart of programming; it is where all the power lies. The doPost()
method you posted can accept ANY HttpServletRequest
as a parameter without knowing specifically which implementation has been passed in - all it knows is that whatever is passed in will honor the HttpServletRequest
interface. One time it may be called with a request that is of a class from a library; the very next time it may be called with a different request, from a class you have written yourself. That is why doPost()
can be used in different contexts; that's what makes it powerful.
1) HttpServletRequest
inherits from the interface ServletRequest
, which contains the getParameter()
method.
2) When a Java method lists an interface as a parameter type, an object that implements that interface must be passed in as a parameter. That object will have code that implements getParameter()
, rather than the 'empty method' you see in the interface. (For example, one such object is HttpServletRequestWrapper
.)
All this information can be found in the JavaDocs, e.g.:
- http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html
The method doesn't take an object of the Interface type "HttpServletRequest". Rather, it accepts any object which implements that Interface. The way a Class implements an Interface is to provide an implementation (ie, code) for the methods that the Interface says are available.
For example, assume we had an Interface for a Dog
interface Dog {
public String bark();
}
For any given class to implement that Interface, it would have to provide it's own version of bark(). Each class implements that method in the way that makes the most sense to it.
class GoldenRetriever implements Dog {
public String bark() {
return "Woof!";
}
}
class YippyDog implements Dog {
public String bark() {
return "yip";
}
}
Edit for comments:
When actually using the bark() command, you would write a method that takes, as input, any class that implements the Dog interface:
public dodog(Dog mydog) {
System.out.println(mydog.bark());
}
Then, you could use that method by creating an Object of any of the types that implement the Dog Interface
public void usedog() {
GoldenRetriever golden = new GoldenRetriever();
dodog(golden);
YippyDog yippy = new YippyDog();
dodog(yippy);
}
The dodog method accepts both of those because they're both instances of a Class that implements the Dog Interface. It can then call the bark() method because it knows they implement it... because they implement the Dog Interface.
I hope that's clearer.
精彩评论