MessageFormat的使用和API說明

先上例子:


    /**  
     * @Title: MessageFormatTest.java
     * @Package com.burns
     * @Description: TODO(用一句話描述該文件做什麼)
     * @author 35725
     * @date 2019年11月27日 上午9:17:04 
     * @version V1.0  
     */
    
package com.burns;

import java.text.MessageFormat;

/**
     * @ClassName: MessageFormatTest
     * @Description: TODO(這裏用一句話描述這個類的作用)
     * @author 35725
     * @date 2019年11月27日
     *
     */

public class MessageFormatTest {
	
	private static String pattern = "當前執行到:{0}{1},下一個執行第{2}{3}";

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(MessageFormat.format(pattern , new Object[]{i,"次",i+1,"次"}));
			
		}
	}

}

輸出結果:

當前執行到:0次,下一個執行第1次
當前執行到:1次,下一個執行第2次
當前執行到:2次,下一個執行第3次
當前執行到:3次,下一個執行第4次
當前執行到:4次,下一個執行第5次
當前執行到:5次,下一個執行第6次
當前執行到:6次,下一個執行第7次
當前執行到:7次,下一個執行第8次
當前執行到:8次,下一個執行第9次
當前執行到:9次,下一個執行第10次

 

MessageFormat 提供了以與語言無關方式生成連接消息的方式。使用此方法構造向終端用戶顯示的消息。

MessageFormat 獲取一組對象,格式化這些對象,然後將格式化後的字符串插入到模式中的適當位置。

注:MessageFormat 不同於其他 Format 類,因爲 MessageFormat 對象是用其構造方法之一創建的(而不是使用 getInstance 樣式的工廠方法創建的)。工廠方法不是必需的,因爲 MessageFormat 本身不實現特定於語言環境的行爲。特定於語言環境的行爲是由所提供的模式和用於已插入參數的子格式來定義的。

模式及其解釋

MessageFormat 使用以下形式的模式:

 MessageFormatPattern:
         String
         MessageFormatPattern FormatElement String

 FormatElement:
         { ArgumentIndex }
         { ArgumentIndex , FormatType }
         { ArgumentIndex , FormatType , FormatStyle }

 FormatType: one of 
         number date time choice

 FormatStyle:
         short
         medium
         long
         full
         integer
         currency
         percent
         SubformatPattern

 String:
         StringPartopt
         String StringPart

 StringPart:
         ''
         ' QuotedString '
         UnquotedString

 SubformatPattern:
         SubformatPatternPartopt
         SubformatPattern SubformatPatternPart

 SubFormatPatternPart:
         ' QuotedPattern '
         UnquotedPattern
 

String 中,"''" 表示單引號。QuotedString 可以包含除單引號之外的任意字符;圍繞的單引號被移除。UnquotedString 可以包含除單引號和左花括號之外的任意字符。因此,格式化後消息字符串爲 "'{0}'" 的字符串可以寫作 "'''{'0}''""'''{0}'''"

SubformatPattern 中,應用了不同的規則。QuotedPattern 可包含除單引號之外的任意字符,但 移除圍繞的單引號,因此它們可以由子格式解釋。例如,"{1,number,$'#',##}" 將產生一個帶井號的數字格式,結果如:"$#31,45"。 UnquotedPattern 可以包含除單引號之外的任意字符,但其中的花括號必須成對出現。例如,"ab {0} de""ab '}' de" 是有效的子格式模式,而 "ab {0'}' de""ab } de" 則是無效的。

 

警告:

不過,在消息格式模式中使用引號的規則在一定程度上顯示混亂。尤其是,本地化程序並不總是清楚單引號是否需要成對。要確保通知本地化程序關於規則的信息,並告訴它們(例如,通過使用資源包源文件中的註釋)MessageFormat 將處理哪些字符串。注意,本地化程序在轉換後的字符串中必須使用單引號,其中原始版本不包含單引號。

ArgumentIndex 值是使用數字 '0' 到 '9' 表示的非負整數,它表示傳遞給 format 方法的 arguments 數組的一個索引,或者表示由 parse 方法返回的結果數組的一個索引。

FormatTypeFormatStyle 值用來創建格式元素的 Format 實例。下表顯示了值如何映射到 Format 實例。表中沒有顯示的組合是非法的。SubformatPattern 必須是所使用的 Format 子類的一個有效的模式字符串。

 

格式類型 格式樣式 創建的子格式
null
number NumberFormat.getInstance(getLocale())
integer NumberFormat.getIntegerInstance(getLocale())
currency NumberFormat.getCurrencyInstance(getLocale())
percent NumberFormat.getPercentInstance(getLocale())
SubformatPattern new DecimalFormat(subformatPattern, DecimalFormatSymbols.getInstance(getLocale()))
date DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getDateInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getDateInstance(DateFormat.LONG, getLocale())
full DateFormat.getDateInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
time DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getTimeInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getTimeInstance(DateFormat.LONG, getLocale())
full DateFormat.getTimeInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
choice SubformatPattern new ChoiceFormat(subformatPattern)

 

用法信息

下面給出一些用法例子。當然,在實際的國際化程序中,消息格式模式和其他靜態字符串將從資源包中獲取。其他參數在運行時動態確定。

第一個例子使用靜態的方法 MessageFormat.format,它在內部創建一個只使用一次的 MessageFormat

 int planet = 7;
 String event = "a disturbance in the Force";

 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     planet, new Date(), event);
 

輸出爲:

 At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
 

下面的例子創建了一個可以重複使用的 MessageFormat 實例:

 int fileCount = 1273;
 String diskName = "MyDisk";
 Object[] testArgs = {new Long(fileCount), diskName};

 MessageFormat form = new MessageFormat(
     "The disk \"{1}\" contains {0} file(s).");

 System.out.println(form.format(testArgs));
 

不同 fileCount 值的輸出:

 The disk "MyDisk" contains 0 file(s).
 The disk "MyDisk" contains 1 file(s).
 The disk "MyDisk" contains 1,273 file(s).
 

對於更復雜的模式,可以使用 ChoiceFormat 來生成正確的單數和複數形式:

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
 double[] filelimits = {0,1,2};
 String[] filepart = {"no files","one file","{0,number} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 form.setFormatByArgumentIndex(0, fileform);

 int fileCount = 1273;
 String diskName = "MyDisk";
 Object[] testArgs = {new Long(fileCount), diskName};

 System.out.println(form.format(testArgs));
 

不同的 fileCount 值的輸出:

 The disk "MyDisk" contains no files.
 The disk "MyDisk" contains one file.
 The disk "MyDisk" contains 1,273 files.
 

如上例所示,可以以編程方式來創建 ChoiceFormat,或使用模式創建。有關更多信息,請參閱 ChoiceFormat

 form.applyPattern(
    "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
 

注:從上面的例子可以看到,由 MessageFormat 中的 ChoiceFormat 所生成的字符串要進行特殊處理;'{' 的出現用來指示子格式,並導致遞歸。如果 MessageFormatChoiceFormat 都是以編程方式創建的(而不是使用字符串模式),那麼要注意不要生成對其自身進行遞歸的格式,這將導致無限循環。

當一個參數在字符串中被多次解析時,最後的匹配將是解析的最終結果。例如,

 MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
 Object[] objs = {new Double(3.1415)};
 String result = mf.format( objs );
 // result now equals "3.14, 3.1"
 objs = null;
 objs = mf.parse(result, new ParsePosition(0));
 // objs now equals {new Double(3.1)}
 

同樣,使用包含同一參數多個匹配項的模式對 MessageFormat 對象進行解析時將返回最後的匹配。例如,

 MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
 String forParsing = "x, y, z";
 Object[] objs = mf.parse(forParsing, new ParsePosition(0));
 // result now equals {new String("z")}
 

同步

消息格式不是同步的。建議爲每個線程創建獨立的格式實例。如果多個線程同時訪問一個格式,則它必須是外部同步的。

 

 

另請參見:

Locale, Format, NumberFormat, DecimalFormat, ChoiceFormat, 序列化表格


嵌套類摘要
static class MessageFormat.Field
          在從 MessageFormat.formatToCharacterIterator 返回的 AttributedCharacterIterator 中定義用作屬性鍵的常量。

 

構造方法摘要
MessageFormat(String pattern)
          構造默認語言環境和指定模式的 MessageFormat。
MessageFormat(String pattern, Locale locale)
          構造指定語言環境和模式的 MessageFormat。

 

方法摘要
 void applyPattern(String pattern)
          設置此消息格式所使用的模式。
 Object clone()
          創建並返回此對象的一個副本。
 boolean equals(Object obj)
          兩個消息格式對象之間的相等性比較
 StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
          格式化一個對象數組,並將 MessageFormat 的模式添加到所提供的 StringBuffer,用格式化後的對象替換格式元素。
 StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)
          格式化一個對象數組,並將 MessageFormat 的模式添加到所提供的 StringBuffer,用格式化後的對象替換格式元素。
static String format(String pattern, Object... arguments)
          創建具有給定模式的 MessageFormat,並用它來格式化給定的參數。
 AttributedCharacterIterator formatToCharacterIterator(Object arguments)
          格式化一個對象數組,並將它們插入 MessageFormat 的模式中,生成一個 AttributedCharacterIterator
 Format[] getFormats()
          獲取用於以前所設置的模式字符串中格式元素的格式。
 Format[] getFormatsByArgumentIndex()
          獲取傳遞給 format 方法或從 parse 方法返回的值的格式。
 Locale getLocale()
          獲取創建或比較子格式時所使用的語言環境。
 int hashCode()
          生成此消息格式對象的哈希碼。
 Object[] parse(String source)
          從給定字符串的開始位置解析文本,以生成一個對象數組。
 Object[] parse(String source, ParsePosition pos)
          解析字符串。
 Object parseObject(String source, ParsePosition pos)
          解析字符串文本,生成一個對象數組。
 void setFormat(int formatElementIndex, Format newFormat)
          使用在以前設置的模式字符串中給定的格式元素索引來設置格式元素使用的格式。
 void setFormatByArgumentIndex(int argumentIndex, Format newFormat)
          設置用於以前所設置的模式字符串中格式元素的格式,其中以前的模式字符串是用給定的參數索引設置的。
 void setFormats(Format[] newFormats)
          設置用於以前所設置的模式字符串中格式元素的格式。
 void setFormatsByArgumentIndex(Format[] newFormats)
          設置傳遞給 format 方法或從 parse 方法返回的值使用的格式。
 void setLocale(Locale locale)
          設置創建或比較子格式時要使用的語言環境。
 String toPattern()
          返回表示消息格式當前狀態的模式。

 

構造方法詳細信息

MessageFormat

public MessageFormat(String pattern)

構造默認語言環境和指定模式的 MessageFormat。構造方法首先設置語言環境,然後解析模式,併爲其包含的格式元素創建子格式列表。在類描述中指定了模式及其解釋。

 

參數:

pattern - 此消息格式的模式

拋出:

IllegalArgumentException - 如果模式無效


MessageFormat

public MessageFormat(String pattern,
                     Locale locale)

構造指定語言環境和模式的 MessageFormat。構造方法首先設置語言環境,然後解析模式,併爲其包含的格式元素創建子格式列表。在類描述中指定了模式及其解釋。

 

參數:

pattern - 此消息格式的模式

locale - 此消息格式的語言環境

拋出:

IllegalArgumentException - 如果模式無效

從以下版本開始:

1.4

方法詳細信息

setLocale

public void setLocale(Locale locale)

設置創建或比較子格式時要使用的語言環境。 這將影響對下面方法的後續調用

  • applyPatterntoPattern 方法的後續調用,如果格式元素指定了格式類型並且因此具有在 applyPattern 方法中創建的子格式,以及
  • formatformatToCharacterIterator 方法的後續調用,如果格式元素未指定格式類型並且因此具有在格式化方法中創建的子格式。

不影響已經創建的子格式。

 

參數:

locale - 創建或比較子格式時所使用的語言環境


getLocale

public Locale getLocale()

獲取創建或比較子格式時所使用的語言環境。

 

返回:

創建或比較子格式時所使用的語言環境


applyPattern

public void applyPattern(String pattern)

設置此消息格式所使用的模式。此方法對模式進行解析,併爲其所包含的格式元素創建子格式列表。在類描述中指定了模式及其解釋。

 

參數:

pattern - 此消息格式的模式

拋出:

IllegalArgumentException - 如果模式無效


toPattern

public String toPattern()

返回表示消息格式當前狀態的模式。字符串是從內部信息創建的,因此不需要與以前應用的模式相等。

 

返回:

表示消息格式當前狀態的模式


setFormatsByArgumentIndex

public void setFormatsByArgumentIndex(Format[] newFormats)

設置傳遞給 format 方法或從 parse 方法返回的值使用的格式。newFormats 中元素的索引對應於以前設置的模式字符串中使用的參數索引。因此,newFormats 中的格式順序對應於傳遞給 format 方法的 arguments 數組或從 parse 方法返回的結果數組中的元素順序。

如果一個參數索引用於模式字符串中的多個格式元素,那麼對應的新的格式將用於所有這樣的格式元素。如果參數索引不用於模式字符串的任何一個格式元素,那麼對應的新的格式將被忽略。如果提供的格式少於所需的格式,那麼只替換其參數索引小於 newFormats.length 的格式。

 

參數:

newFormats - 要使用的新格式

拋出:

NullPointerException - 如果 newFormats 爲 null

從以下版本開始:

1.4


setFormats

public void setFormats(Format[] newFormats)

設置用於以前所設置的模式字符串中格式元素的格式。newFormats 中的格式順序對應於模式字符串中的格式元素的順序。

如果提供的格式多於模式字符串所需的格式,那麼剩餘的格式將被忽略。如果提供的格式少於所需的格式,那麼只替換前 newFormats.length 個格式。

由於在本地化期間,模式字符串中的格式元素順序經常發生變化,因此最好使用 setFormatsByArgumentIndex 方法,此方法假定格式順序對應於傳遞給 format 方法或從 parse 方法返回的結果數組的 arguments 數組中的元素順序。

 

參數:

newFormats - 要使用的新格式

拋出:

NullPointerException - 如果 newFormats 爲 null


setFormatByArgumentIndex

public void setFormatByArgumentIndex(int argumentIndex,
                                     Format newFormat)

設置用於以前所設置的模式字符串中格式元素的格式,其中以前的模式字符串是用給定的參數索引設置的。參數索引是格式元素定義的一部分,它表示傳遞給 format 方法或從 parse 方法返回的結果數組的 arguments 數組的索引。

如果參數索引用於模式字符串中的多個格式元素,那麼新的格式將用於所有這樣的格式元素。如果參數索引沒有用於模式字符串的任何格式元素,那麼新的格式將被忽略。

 

參數:

argumentIndex - 要用於新的格式的參數索引

newFormat - 要使用的新格式

從以下版本開始:

1.4


setFormat

public void setFormat(int formatElementIndex,
                      Format newFormat)

使用在以前設置的模式字符串中給定的格式元素索引來設置格式元素使用的格式。格式元素索引是從模式字符串起始位置開始計數的、從 0 開始的數字。

由於在本地化期間,模式字符串中的格式元素的順序經常發生變化,因此最好使用 setFormatByArgumentIndex 方法,此方法根據它們所指定的參數索引來訪問格式元素。

 

參數:

formatElementIndex - 模式中的格式元素的索引

newFormat - 要用於指定格式元素的格式

拋出:

ArrayIndexOutOfBoundsException - 如果 formatElementIndex 等於或大於模式字符串中格式元素的個數


getFormatsByArgumentIndex

public Format[] getFormatsByArgumentIndex()

獲取傳遞給 format 方法或從 parse 方法返回的值的格式。返回的數組中的元素的索引對應於以前設置模式字符串中所使用的參數索引。因此,返回的數組中的格式順序對應於傳遞給 format 方法或從 parse 方法返回的 arguments 數組中的元素順序。

如果一個參數索引用於模式字符串中的多個格式元素,那麼在數組中將返回用於最後一個這樣的元素的格式。如果參數索引不用於模式字符串中的任何一個格式元素,那麼在數組中將返回 null。

 

返回:

用於模式中參數的格式

從以下版本開始:

1.4


getFormats

public Format[] getFormats()

獲取用於以前所設置的模式字符串中格式元素的格式。返回的數組中的格式順序對應於模式字符串中的格式元素順序。

由於在本地化期間,模式字符串中的格式元素的順序經常發生變化,因此最好使用 getFormatsByArgumentIndex 方法,此方法假定格式的順序對應於傳遞給 format 方法的 arguments 數組或從 parse 方法返回的結果數組中的元素順序。

 

返回:

用於模式中格式元素的格式


format

public final StringBuffer format(Object[] arguments,
                                 StringBuffer result,
                                 FieldPosition pos)

格式化一個對象數組,並將 MessageFormat 的模式添加到所提供的 StringBuffer,用格式化後的對象替換格式元素。

替換個別格式元素的文本從格式元素和位於格式元素參數索引處的 arguments 元素的當前子格式中派生出來,如下表中第一個匹配行所示。如果 argumentsnull,或者元素的個數少於 argumentIndex+1,則參數是不可用的

 

子格式 參數 格式化文本
所有 不可用 "{" + argumentIndex + "}"
所有 null "null"
instanceof ChoiceFormat 所有 subformat.format(argument).indexOf('{') >= 0 ?
(new MessageFormat(subformat.format(argument), getLocale())).format(argument) : subformat.format(argument)
!= null 所有 subformat.format(argument)
null instanceof Number NumberFormat.getInstance(getLocale()).format(argument)
null instanceof Date DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)
null instanceof String argument
null 所有 argument.toString()

如果 pos 不爲 null,且引用 Field.ARGUMENT,那麼將返回第一個格式化字符串的位置。

 

參數:

arguments - 要被格式化和替換的對象數組。

result - 添加文本的位置。

pos - 輸入時:如果需要,是一個對齊字段。輸出時:爲對齊字段的偏移量。

拋出:

IllegalArgumentException - 如果 arguments 數組中的參數不是使用該參數的格式元素期望的類型。


format

public static String format(String pattern,
                            Object... arguments)

創建具有給定模式的 MessageFormat,並用它來格式化給定的參數。這等效於

(new MessageFormat(pattern)).format(arguments, new StringBuffer(), null).toString()

 

拋出:

IllegalArgumentException - 如果模式無效,或者 arguments 數組中的參數不是使用該參數的格式元素期望的類型。


format

public final StringBuffer format(Object arguments,
                                 StringBuffer result,
                                 FieldPosition pos)

格式化一個對象數組,並將 MessageFormat 的模式添加到所提供的 StringBuffer,用格式化後的對象替換格式元素。這等效於

format((Object[]) arguments, result, pos)

 

指定者:

Format 中的 format

參數:

arguments - 要被格式化和替換的對象數組。

result - 添加文本的位置。

pos - 輸入時:如果需要,是一個對齊字段。輸出時:爲對齊字段的偏移量。

返回:

添加了格式化文本並作爲 toAppendTo 傳入的字符串緩衝區

拋出:

IllegalArgumentException - 如果 arguments 數組中的參數不是使用該參數的格式元素期望的類型。


formatToCharacterIterator

public AttributedCharacterIterator formatToCharacterIterator(Object arguments)

格式化一個對象數組,並將它們插入 MessageFormat 的模式中,生成一個 AttributedCharacterIterator。可以使用返回的 AttributedCharacterIterator 來生成得到的字符串,以及確定關於得到字符串的信息。

返回的 AttributedCharacterIterator 的文本與以下語句返回的結果相同

format(arguments, new StringBuffer(), null).toString()

此外,AttributedCharacterIterator 至少包含一些屬性,指示從 arguments 數組的某個參數生成文本的位置。這些屬性的鍵是 MessageFormat.Field 類型的,其值是 Integer 對象,指示參數的 arguments 數組中的索引,其中文本是從此索引生成的。

MessageFormat 所使用的底層 Format 實例的屬性/值也將存放在得到的 AttributedCharacterIterator 中。這不僅允許查找參數被存放在得到的 String 中的位置,而且允許查找它依次包含哪些字段。

 

覆蓋:

Format 中的 formatToCharacterIterator

參數:

arguments - 要被格式化和替換的對象數組。

返回:

描述格式化後的值的 AttributedCharacterIterator。

拋出:

NullPointerException - 如果 arguments 爲 null。

IllegalArgumentException - 如果 arguments 數組中的參數不是使用該參數的格式元素期望的類型。

從以下版本開始:

1.4


parse

public Object[] parse(String source,
                      ParsePosition pos)

解析字符串。

警告:在許多情況下解析可能失敗。例如:

  • 如果某個參數未出現在模式中。
  • 如果丟失了參數的格式信息,例如對於一個格式化選項,其中一個大數格式化爲 "many"。
  • 尚未處理遞歸(其中替換後的字符串包含 {n} 引用)。
  • 當解析的某個部分不明確時,將不能總找到一個匹配(或者正確的匹配)。例如,如果模式 "{1},{2}" 用於字符串參數 {"a,b", "c"},它將格式化爲 "a,b,c"。當解析結果時,它將返回 {"a", "b,c"}。
  • 如果字符串中的參數被解析多次,那麼將保留最後一次的解析結果。

當解析失敗時,使用 ParsePosition.getErrorIndex() 來查找字符串的哪個位置導致瞭解析失敗。返回的錯誤索引是要用來與字符串比較的子模式的起始偏移量。例如,如果解析的字符串 "AAA {0} BBB" 與模式 "AAD {0} BBB" 進行比較,則錯誤索引爲 0。當發生錯誤時,對此方法的調用將返回 null。如果源爲 null,則返回一個空數組。

 


parse

public Object[] parse(String source)
               throws ParseException

從給定字符串的開始位置解析文本,以生成一個對象數組。此方法不可以使用給定字符串的全部文本。

有關消息解析的更多信息,請參閱 parse(String, ParsePosition) 方法。

 

參數:

source - 必須解析其開頭的 String

返回:

從字符串進行解析的 Object 數組。

拋出:

ParseException - 如果無法解析指定字符串的開頭。


parseObject

public Object parseObject(String source,
                          ParsePosition pos)

解析字符串文本,生成一個對象數組。

此方法試圖解析從 pos 給定的索引處開始的文本。如果解析成功,則將 pos 的索引更新到所解析的最後一個字符後面的索引(不必對直到字符串結尾的所有字符進行解析),並返回解析後的對象數組。更新的 pos 可以用來指示下次調用此方法的起始點。如果發生錯誤,則不更改 pos 的索引,並將 pos 的錯誤索引設置爲發生錯誤處的字符索引,並且返回 null。

有關消息解析的更多信息,請參閱 parse(String, ParsePosition) 方法。

 

指定者:

Format 中的 parseObject

參數:

source - 應該解析其中一部分的 String

pos - 具有以上所述的索引和錯誤索引信息的 ParsePosition 對象。

返回:

從字符串進行解析的 Object 數組。如果發生錯誤,則返回 null。

拋出:

NullPointerException - 如果 pos 爲 null。


clone

public Object clone()

創建並返回此對象的一個副本。

 

覆蓋:

Format 中的 clone

返回:

此實例的一個副本。

另請參見:

Cloneable


equals

public boolean equals(Object obj)

兩個消息格式對象之間的相等性比較

 

覆蓋:

Object 中的 equals

參數:

obj - 要與之比較的引用對象。

返回:

如果此對象與 obj 參數相同,則返回 true;否則返回 false

另請參見:

Object.hashCode(), Hashtable


hashCode

public int hashCode()

生成此消息格式對象的哈希碼。

 

覆蓋:

Object 中的 hashCode

返回:

此對象的一個哈希碼值。

另請參見:

Object.equals(java.lang.Object), Hashtable

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章