public class Mortgage { private float rate; private int years; .... public String toString() { return new ToStringBuilder(this).append("rate", this.rate) .append("years", this.years). toString(); } } 为什么使用这个函数那?因为它使用通用的方法处理所有的数据类型,恰当的返回null,同时可以控制对象和聚集的细节层次。这对于所有builder中的函数有效,而且语法同上所示相近。
public final class OnTV extends Enum { public static final OnTV IDOL = new OnTV("Idol"); public static final OnTV SURVIVOR = new OnTV("Survivor"); public static final OnTV SEINFELD = new OnTV("Seinfeld");
private OnTV(String show) { super(show); }
public static OnTV getEnum(String show){ return (OnTV) getEnum(OnTV.class, show); }
public static Map getEnumMap() { return getEnumMap(OnTV.class); }
public static List getEnumList() { return getEnumList(OnTV.class); }
public static Iterator iterator() { return iterator(OnTV.class); }
while(itr.hasNext()) { FileItem item = (FileItem) itr.next(); // check if the current item is a form field or an uploaded file if(item.isFormField()) { // get the name of the field String fieldName = item.getFieldName(); // if it is name, we can set it in request to thank the user if(fieldName.equals("name")) request.setAttribute("msg", "Thank You: " + item.getString()); } else {
// the item must be an uploaded file save it to disk. Note that there // seems to be a bug in item.getName() as it returns the full path on // the client's machine for the uploaded file name, instead of the file // name only. To overcome that, I have used a workaround using // fullFile.getName(). File fullFile = new File(item.getName()); File savedFile = new File(getServletContext().getRealPath("/"), fullFile.getName()); item.write(savedFile); }