class Address { private enum Component { NUMBER, STREET, STATE, COUNTRY } private Map<Component, String> componentToValue = ...; }
I would like my class to contain two methods:
What is the best practice for Object.toString ()? Is this primary for # 1 or # 2? Is there a best practice to label these methods?
Format the address in the same way in the SMS message and on the HTML page? Would you format it also in English, French and Japanese?
, : , , . , , HtmlI18nedAddress, toString .
Date vs SimpleDateFormat. Date SimpleDateFormat .
Date
SimpleDateFormat
, . ToString() .
: ToString(), Parse ( ) ( , ). , , , ToString().
Apache Commons ToStringBuilder http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/builder/ToStringBuilder.html , , , .
, .:
@Override public String toString() { if(debug) { return debugAddrString() } return normalAddrString(); }
ToString . , Object; Object ? , , , . .
Object
: IDE ToString .
, , Debug, , :
public String toString(boolean debug) { if (debug) return debugStringVersion; else return humanVersion; } public String toString() { return toString(Util.DEBUG); }
Of course, this assumes that you have a utility class with a debug flag in it.
Javadoc says:
In general, the toString method returns a string that "textually represents" this object. should be a concise but informative presentation that is easy for a person to read.
So I go with 2.