日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSFormattable

Formattable

public interface Formattable

Formattable 接口必須由任何需要使用 Formatter 的 's' 轉(zhuǎn)換說(shuō)明符執(zhí)行自定義格式化的類來(lái)實(shí)現(xiàn)。 該接口允許對(duì)任意對(duì)象進(jìn)行格式化的基本控制。 例如,以下類根據(jù)標(biāo)志和長(zhǎng)度限制打印出股票名稱的不同表示:


 
   import java.nio.CharBuffer;
   import java.util.Formatter;
   import java.util.Formattable;
   import java.util.Locale;
   import static java.util.FormattableFlags.*;


   ...


   public class StockName implements Formattable {
       private String symbol, companyName, frenchCompanyName;
       public StockName(String symbol, String companyName,
                        String frenchCompanyName) {
           ...
       }


       ...


       public void formatTo(Formatter fmt, int f, int width, int precision) {
           StringBuilder sb = new StringBuilder();


           // decide form of name
           String name = companyName;
           if (fmt.locale().equals(Locale.FRANCE))
               name = frenchCompanyName;
           boolean alternate = (f & ALTERNATE) == ALTERNATE;
           boolean usesymbol = alternate || (precision != -1 && precision < 10);
           String out = (usesymbol ? symbol : name);


           // apply precision
           if (precision == -1 || out.length() < precision) {
               // write it all
               sb.append(out);
           } else {
               sb.append(out.substring(0, precision - 1)).append('*');
           }


           // apply width and justification
           int len = sb.length();
           if (len < width)
               for (int i = 0; i < width - len; i++)
                   if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
                       sb.append(' ');
                   else
                       sb.insert(0, ' ');


           fmt.format(sb.toString());
       }


       public String toString() {
           return String.format("%s - %s", symbol, companyName);
       }
   }

 

當(dāng)與 Formatter 結(jié)合使用時(shí),上述類為各種格式字符串生成以下輸出。


 
   Formatter fmt = new Formatter();
   StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
                                "Fruit Titanesque, Inc.");
   fmt.format("%s", sn);                   //   -> "Huge Fruit, Inc."
   fmt.format("%s", sn.toString());        //   -> "HUGE - Huge Fruit, Inc."
   fmt.format("%#s", sn);                  //   -> "HUGE"
   fmt.format("%-10.8s", sn);              //   -> "HUGE      "
   fmt.format("%.12s", sn);                //   -> "Huge Fruit,*"
   fmt.format(Locale.FRANCE, "%25s", sn);  //   -> "   Fruit Titanesque, Inc."

 

格式化表對(duì)于多線程訪問不一定是安全的。 線程安全是可選的,可以由擴(kuò)展和實(shí)現(xiàn)此接口的類強(qiáng)制執(zhí)行。

除非另有說(shuō)明,否則將 null 參數(shù)傳遞給此接口中的任何方法都將導(dǎo)致拋出 NullPointerException。

方法總結(jié)

修飾符和類型 方法 描述
void formatTo(Formatter formatter, int flags, int width, int precision) 使用提供的 Formatter 格式化對(duì)象。

方法詳情

formatTo

void formatTo(Formatter formatter, int flags, int width, int precision)

使用提供的 Formatter 格式化對(duì)象。

參數(shù):

參數(shù)名稱 參數(shù)描述
formatter 格式化程序。 實(shí)現(xiàn)類可以調(diào)用 Formatter#out() 或 Formatter#locale() 來(lái)分別獲取此格式化程序使用的 Appendable 或 Locale。
flags 標(biāo)志修改輸出格式。 該值被解釋為位掩碼。 可以設(shè)置以下標(biāo)志的任意組合:FormattableFlags#LEFT_JUSTIFY、FormattableFlags#UPPERCASE 和 FormattableFlags#ALTERNATE。 如果沒有設(shè)置標(biāo)志,則應(yīng)用實(shí)現(xiàn)類的默認(rèn)格式。
width 要寫入輸出的最小字符數(shù)。 如果轉(zhuǎn)換后的值的長(zhǎng)度小于寬度,則輸出將用 ' ' 填充,直到字符總數(shù)等于寬度。 默認(rèn)情況下,填充位于開頭。 如果設(shè)置了 FormattableFlags#LEFT_JUSTIFY 標(biāo)志,則填充將在末尾。 如果寬度為-1,則沒有最小值。
precision 要寫入輸出的最大字符數(shù)。 精度在寬度之前應(yīng)用,因此即使寬度大于精度,輸出也會(huì)被截?cái)酁榫茸址?nbsp;如果精度為 -1,則對(duì)字符數(shù)沒有明確限制。

Throws:

Throw名稱 Throw描述
IllegalFormatException 如果任何參數(shù)無(wú)效。

新聞標(biāo)題:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSFormattable
網(wǎng)站地址:http://www.5511xx.com/article/cospoih.html