在Java中,我可以合并两个使用JspWriter和另一个PrintWriter的类似函数吗?

发布于 2024-09-25 18:00:33 字数 2327 浏览 3 评论 0原文

我有以下类,正如您将看到的,它有一个相当多余的 formatNameAndAddress 方法:

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>");
 }
}

我想重写该类以使用通用方法,例如:

     // 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,有时会以错误的方式转换它。

我想我需要做的是以某种方式将对象类型作为变量传递,然后使用该变量来确定如何进行转换。这可能吗?如果是这样,怎么办?如果没有,什么是好的解决方案?

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>");
 }

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪都笑了 2024-10-02 18:00:33

这可能会起作用:

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

This will probably work:

public void formatNameAndAddress(JspWriter out) throws java.io.IOException {
    formatNameAndAddress(new PrintWriter(out));
}
若沐 2024-10-02 18:00:33

您使用这些方法混淆了两个不同的任务,并打破了面向对象的专业化原则。也就是说,您拥有负责格式化两种类型的字符串之一的方法...并负责将它们发送到两种类型的输出目标之一。

更好的方法可能是使您的方法更加专业化。也就是说,让它们只负责构建“名称”字符串或“名称和地址”字符串...并返回 String 作为方法的返回类型。

在代码中调用这些方法的地方,您显然已经有了一个 JspWriterPrintWriter 对象...因为现在您将它作为方法参数。因此,简单地将该对象保留在代码中的位置,并让它打印出由与输出无关的专用方法返回的 String 会更干净。

You're kind of muddling two different tasks with these methods, and breaking the OO principle of specialization. That is, you have methods which are responsible for formatting one of two types of strings... AND responsible for sending them to one of two types of output targets.

A better approach might be to make your methods more specialized. That is, have them ONLY be responsible for building a "Name" string or a "Name and Address" string... and return String as the methods' return type.

At the point in code where you're invoking these methods, you obviously already have a JspWriter or a PrintWriter object... because right now you're passing it as an method argument. So it would be cleaner to simply leave that object where it is in the code, and have it print out the String which is returned by your specialized output-agnostic method.

尐籹人 2024-10-02 18:00:33

如果您想将对象转换为正确的 Writer,您可以尝试类似的方法:

private void genericFormatNameAndAddress(Object out){
    if (obj instanceof Printwriter){
        //cast to printwriter
    } else {
        //cast to JspWriter
    }

Barkers 解决方案似乎更适合,因为 Printwriter 可以由 JspWriter 构造而成。

If you want to cast the object to the correct Writer you can try something like:

private void genericFormatNameAndAddress(Object out){
    if (obj instanceof Printwriter){
        //cast to printwriter
    } else {
        //cast to JspWriter
    }

Barkers solution seems to be better suited because a Printwriter can be constructed out of a JspWriter.

人事已非 2024-10-02 18:00:33

JspWriterPrintWriter 都是 java.io.Writer 的子类。由于您不使用两者特定的任何功能,因此您可以将该方法声明为采用 java.io.Writer

public void formatNameAndAddress(Writer out) throws java.io.IOException
{
    [...]

然后传递 JspWriter 或根据需要 PrintWriter

编辑:如果不实际修改代码,这将不起作用,因为正如其他人指出的那样,Writer没有printprintln 方法,而 JspWriterPrintWriter 都提供它们。

Both JspWriter and PrintWriter are subclasses of java.io.Writer. Since you don't use any functionality that is specific to any of the two, you can declare the method as taking a java.io.Writer:

public void formatNameAndAddress(Writer out) throws java.io.IOException
{
    [...]

Then pass a JspWriter or PrintWriter as required.

Edit: This won't work without actually modifying the code since, as pointed out by others, Writer doesn't have print and println methods, whereas JspWriter and PrintWriter both provide them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文