当前位置: 代码迷 >> J2SE >> 继承转为接口的有关问题
  详细解决方案

继承转为接口的有关问题

热度:6181   发布时间:2013-02-25 00:00:00.0
继承转为接口的问题
Class Main.java

package System;

import javax.servlet.http.HttpServlet;

public class Main extends HttpServlet {
  public String rs;
   
  public void A(String Test){
  this.B();
  }
   
  private void B(){
  rs="Test String B<br />";
  }
   
  public void C(){
  rs="Test Sting C<br />";
  }
   
  public void D(){
  rs="Test String D<br />";
  }
   
  @Override
  public String toString(){
  return rs.toString();
  }
}

Class index.java
package web;

import System.Main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="index", urlPatterns={"/index"})
public class index extends Main {
   
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  this.A("index");
  out.print(this.toString());
  this.C();
  out.print(this.toString());
  try {
  } finally { 
  out.close();
  }
  } 
   
  @Override
  public void C(){
  this.rs="Test String Main.C<br />";
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
  } 

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
  }

  @Override
  public String getServletInfo() {
  return "Short description";
  }

}

Class NewServlet.java

package web;

import System.Main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="NewServlet", urlPatterns={"/NewServlet"})
public class NewServlet extends Main {
   
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
  this.A("aaaaaaa");
  this.D();
  out.print(this.toString());
  this.C();
  out.print(this.toString());
  } finally { 
  out.close();
  }
  } 
   
  @Override
  public void D(){
  this.rs="Test String NewServlet.D<br />";
  }

  @Override
  相关解决方案