日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Item9:总是要改写toString

發布時間:2023/12/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Item9:总是要改写toString 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**toString()方法的約定:結果應是一個簡明但易于讀懂。建議所有子類都重寫此方法。

?* 當一個println,字符串連接(+)操作符或assert的時候,toString方法會自動被調用。

?*

?* 在實際應用中,toString方法應該返回對象中包含的所有令人感興趣的信息或摘要信息。

?* 不管你是否決定指定返回值的格式,都應該在文檔中明確地表明你的意圖。

?* 另外,為toString返回值中包含的所有信息都提供一種編程訪問途徑是一個好的做法,

?* 這樣可以讓程序直接得到特定的數據,則無需要費力來解析這個字符串來獲得。

?*

?*/

public class PhoneNumber {

??? private final short areaCode;

??? private final short exchange;

??? private final short extension;

?

??? public PhoneNumber(int areaCode, int exchange,

?????????????????????? int extension) {

??????? rangeCheck(areaCode,?? 999, "area code");

??????? rangeCheck(exchange,?? 999, "exchange");

??????? rangeCheck(extension, 9999, "extension");

??????? this.areaCode? = (short) areaCode;

??????? this.exchange? = (short) exchange;

??????? this.extension = (short) extension;

??? }

?

??? private static void rangeCheck(int arg, int max,

????????????????????????????????? ?String name) {

??????? if (arg < 0 || arg > max)

?????????? throw new IllegalArgumentException(name +": " + arg);

??? }

?

??? public boolean equals(Object o) {

??????? if (o == this)

??????????? return true;

??????? if (!(o instanceof PhoneNumber))

??????????? return false;

??????? PhoneNumber pn = (PhoneNumber)o;

??????? return pn.extension == extension &&

?????????????? pn.exchange? == exchange? &&

?????????????? pn.areaCode? == areaCode;

??? }

?

??? // Page 39

??? public int hashCode() {

??????? int result = 17;

??????? result = 37*result + areaCode;

??????? result = 37*result + exchange;

??????? result = 37*result + extension;

??????? return result;

??? }

?

??? // Page 43

??? /**

???? * Returns the string representation of this phone number.

???? * The string consists of fourteen characters whose format

???? * is "(XXX) YYY-ZZZZ", where XXX is the area code, YYY is

???? * the extension, and ZZZZ is the exchange.? (Each of the

???? * capital letters represents a single decimal digit.)

???? *

???? * If any of the three parts of this phone number is too small

???? * to fill up its field, the field is padded with leading zeros.

???? *? For example, if the value of the exchange is 123, the last

???? * four characters of the string representation will be "0123".

???? *

???? * Note that there is a single space separating the closing

???? * parenthesis after the area code from the first digit of the

???? * exchange.

???? */

??? public String toString() {

??????? return "(" + toPaddedString(areaCode, 3) + ") " +

??????????????? toPaddedString(exchange,? 3) + "-" +

??????????????? toPaddedString(extension, 4);

??? }

?

??? /**

???? * Translates an int to a string of the specified length,

???? * padded with leading zeros.? Assumes i >= 0,

???? * 1 <= length <= 10, and Integer.toString(i) <= length.

???? */

??? private static String toPaddedString(int i, int length) {

??????? String s = Integer.toString(i);

??????? return ZEROS[length - s.length()] + s;

??? }

??? private static String[] ZEROS =

??????? {"", "0", "00", "000", "0000", "00000",

???????? "000000", "0000000", "00000000", "000000000"};

??

?

??? public static void main(String[] args) {

??????? System.out.println("Failed to connect: " +

?????????????????????????? new PhoneNumber(408, 867, 5309));

??? }

}

?

轉載于:https://www.cnblogs.com/wxf0701/archive/2008/04/24/1169812.html

總結

以上是生活随笔為你收集整理的Item9:总是要改写toString的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。