当前位置: 技术问答>java相关
关于接口的问题?
来源: 互联网 发布时间:2017-04-04
本文导语: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ...
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
}
}
response 是 HttpServletResponse 对象
response.setContentType("text/html");
然而 HttpServletResponse 是接口 按照接口的定义接口是不能实现方法的
请问如何理解这个问题
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
}
}
response 是 HttpServletResponse 对象
response.setContentType("text/html");
然而 HttpServletResponse 是接口 按照接口的定义接口是不能实现方法的
请问如何理解这个问题
|
interface不能直接实例化,你看到的都是interface的子类的实例啦
只要你有一个类继承了一个接口,(当然在类里接口就实例话了),那么你就可以把这个对象返回给那个接口,然后调用接口的方法,(其实就是你在类里实例化的那个)
http://expert.csdn.net/Expert/topic/1226/1226948.xml?temp=3.168887E-02
只要你有一个类继承了一个接口,(当然在类里接口就实例话了),那么你就可以把这个对象返回给那个接口,然后调用接口的方法,(其实就是你在类里实例化的那个)
http://expert.csdn.net/Expert/topic/1226/1226948.xml?temp=3.168887E-02