JavaRush /Java Blog /Random-TW /訊息格式類
Виталий Рыжаков
等級 40
Москва

訊息格式類

在 Random-TW 群組發布
在這篇文章中我想談談MessageFormat任務2412(task2412)中使用的類別。 訊息格式類別 - 1該類別MessageFormat旨在創建字串。這個 Java 類別採用一組對象,對它們進行格式化,然後將格式化的字串插入到模板中的適當位置。這是靜態方法的一種替代(甚至補充)String.format。首先,使用這個 Java 類別的簡單範例,無需建立對象,而是使用靜態方法:
int planet = 7;
String event = "a disturbance in the Force";
String result = MessageFormat.format(
        "At {0, time, medium} on {0, date}, there was {1} on planet {2, number, integer}.",
        new Date(), event, planet);
System.out.println(result);
這裡呼叫一個靜態方法MessageFormat.format,字串模板以及實際上將插入到由括號分隔的位置的物件作為參數傳遞給該方法{}。物件起始位置在括號中指定0,以及格式類型(如果有)。輸出將是這樣的:
At 21:25:54 on 28 апр. 2018 г., there was a disturbance in the Force on planet 7.
下面的範例已經建立了該類別的一個物件MessageFormat
int fileCount = 1273;
String diskName = "MyDisk";
Object[] testArgs = {fileCount, diskName};

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

System.out.println(form.format(testArgs));
建立類別物件時,MessageFormat會將字串模板傳遞給其建構函數。接下來,當呼叫物件的方法時format,將插入字串模板的物件陣列作為參數傳遞到那裡。輸出將是這樣的:
The disk "MyDisk" contains 1 273 file(s).
還可以確保根據變數的值選擇所需的文字。運算符的一種實現if...else,僅使用類別ChoiceFormat。這是代碼:
MessageFormat form = new MessageFormat("Я могу {1} {0}.");
int count = 2;
String exercise = "подтянуться";
Object[] testArgs = {count, exercise};

double[] filelimits = {0,2,5};
String[] filepart = {"{0} раз","{0} раза","{0} раз"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
form.setFormatByArgumentIndex(0, fileform);

System.out.println(form.format(testArgs));
此程式碼根據變數的值count更改該行的輸出,如下所示:
  • 如果count = 1結論是這樣的

    Я могу подтянуться 1 раз.
  • 如果count = 2結論是這樣的

    Я могу подтянуться 2 раза.
  • 如果count = 7結論是這樣的

    Я могу подтянуться 7 раз.
此程式碼建立一個數組double[] filelimits,指定字串輸出將更改的值​​的限制。該數組String[] filepart指示可以使用的字串的各種變體。filepart[j]從變數值中選擇行選項的條件確定如下:如果 ,則選擇該選項filelimits[j] =< count < filelimits[j+1]。接下來,建立一個物件並將陣列ChoiceFormat fileform傳遞給其建構函數。透過該方法,我們告訴對象,當呼叫該方法時,對於行模板中的索引 0,使用對像中指定的格式。美好的。原則上,您可能已經有了永遠不要使用 Java 類別的想法,但要知道這並不是所有的花里胡哨。有這樣的程式碼: double[] filelimitsString[] filepartform.setFormatByArgumentIndex(0, fileform)MessageFormat formformatChoiceFormat fileformMessageFormat
MessageFormat pattform = new MessageFormat("There {0} on {1}.\n{2} {2}");
int count = 0;
Date date = new Date();
Object[] testArgs = {count, "ADisk", date, date};

double[] filelimits = {0,1,2};
String[] filepart = {"are no files","is one file","are {0} files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);

Format[] testFormats
        = {fileform, null, DateFormat.getDateInstance(DateFormat.SHORT), DateFormat.getTimeInstance(DateFormat.SHORT)};
pattform.setFormats(testFormats);

System.out.println(pattform.format(testArgs));
這是數組的主要特徵Format[] testFormatsFormat在此數組中,我們新增負責格式化數組中定義的物件的物件(實作抽象類別) Object[] testArgsFormat[] testFormats如果物件不需要格式化,則null. pattform.setFormats(testFormats)接下來,我們使用該方法告訴物件MessageFormat pattform行範本中的所有索引都應使用數組中定義的格式Format[] testFormats。對於此範例,輸出將是:
There are no files on ADisk.
28.04.18 22:10
就是這樣,希望你在解決2412題時不會遇到任何困難。
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION