在Java中,我可以合并两个类似的函数,其中使用JspWriter和其他PrintWriter? [英] In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

查看:93
本文介绍了在Java中,我可以合并两个类似的函数,其中使用JspWriter和其他PrintWriter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类,你会看到一个相当多的formatNameAndAddress方法:

I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method:

package hu.flux.helper;
import java.io.PrintWriter;

import javax.servlet.jsp.JspWriter;

// A holder for formatting data 
public class NameAndAddress 
{
 public String firstName;
 public String middleName;
 public String lastName;
 public String address1;
 public String address2;
 public String city;
 public String state;
 public String zip;

 // Print out the name and address.
 public void formatNameAndAddress(JspWriter out)
  throws java.io.IOException
  {
   out.println("<PRE>");
    out.print(firstName);

    // Print the middle name only if it contains data.
    if ((middleName != null) && (middleName.length() > 0)) 
     {out.print(" " + middleName);}

    out.println(" " + lastName);

    out.println(" " + address1);

    if ((address2 != null) && (address2.length() > 0))
     out.println(" " + address2);

    out.println(city + ", " + state + " " + zip);
   out.println("</PRE>");
  }

 public void formatName(PrintWriter out) 
 {
  out.println("<PRE>");
  out.print(firstName);

  // Print the middle name only if it contains data.
  if ((middleName != null) && (middleName.length() > 0)) 
   {out.print(" " + middleName);}

  out.println(" " + lastName);

  out.println(" " + address1);

  if ((address2 != null) && (address2.length() > 0))
   out.println(" " + address2);

  out.println(city + ", " + state + " " + zip);
  out.println("</PRE>");
 }
}

我想重写类来使用通用方法如:

I'd like to rewrite the class to use a generic method like:

     // Print out the name and address.
 private void genericFormatNameAndAddress(Object out)
 {
  out.println("<PRE>");
   out.print(firstName);

   // Print the middle name only if it contains data.
   if ((middleName != null) && (middleName.length() > 0)) 
    {out.print(" " + middleName);}

   out.println(" " + lastName);

   out.println(" " + address1);

   if ((address2 != null) && (address2.length() > 0))
    out.println(" " + address2);

   out.println(city + ", " + state + " " + zip);
  out.println("</PRE>");
 }

但是,我不能这样做,因为Object不有print()和println()方法。如果我把输出转换为JspWriter或PrintWriter,我有时会把它转换为错误的方式。

But, I can't do this exactly like this because Object doesn't have print() and println() methods. If I cast the output to either JspWriter or PrintWriter, I'd be casting it the wrong way sometimes.

我想象我需要做的是以某种方式传递对象类型作为变量,然后使用该变量确定如何投射。这可能吗?如果是,如何?

I imagine what I need to do is somehow pass the object type as a variable and then use the variable to determine how to cast. Is this possible? If so, how? If not, what would be a good solution?

推荐答案

这可能会奏效:

public void formatNameAndAddress(JspWriter out) throws java.io.IOException {
    formatNameAndAddress(new PrintWriter(out));
}

这篇关于在Java中,我可以合并两个类似的函数,其中使用JspWriter和其他PrintWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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