在Java中,我可以合并两个使用JspWriter和另一个PrintWriter的类似函数吗?
我有以下类,正如您将看到的,它有一个相当多余的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能会起作用:
This will probably work:
您使用这些方法混淆了两个不同的任务,并打破了面向对象的专业化原则。也就是说,您拥有负责格式化两种类型的字符串之一的方法...并负责将它们发送到两种类型的输出目标之一。
更好的方法可能是使您的方法更加专业化。也就是说,让它们只负责构建“名称”字符串或“名称和地址”字符串...并返回
String
作为方法的返回类型。在代码中调用这些方法的地方,您显然已经有了一个
JspWriter
或PrintWriter
对象...因为现在您将它作为方法参数。因此,简单地将该对象保留在代码中的位置,并让它打印出由与输出无关的专用方法返回的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 aPrintWriter
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 theString
which is returned by your specialized output-agnostic method.如果您想将对象转换为正确的 Writer,您可以尝试类似的方法:
Barkers 解决方案似乎更适合,因为 Printwriter 可以由 JspWriter 构造而成。
If you want to cast the object to the correct Writer you can try something like:
Barkers solution seems to be better suited because a Printwriter can be constructed out of a JspWriter.
JspWriter
和PrintWriter
都是java.io.Writer
的子类。由于您不使用两者特定的任何功能,因此您可以将该方法声明为采用java.io.Writer
:然后传递
JspWriter
或根据需要PrintWriter
。编辑:如果不实际修改代码,这将不起作用,因为正如其他人指出的那样,
Writer
没有print
和println
方法,而JspWriter
和PrintWriter
都提供它们。Both
JspWriter
andPrintWriter
are subclasses ofjava.io.Writer
. Since you don't use any functionality that is specific to any of the two, you can declare the method as taking ajava.io.Writer
:Then pass a
JspWriter
orPrintWriter
as required.Edit: This won't work without actually modifying the code since, as pointed out by others,
Writer
doesn't haveprint
andprintln
methods, whereasJspWriter
andPrintWriter
both provide them.