當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.ListResourceBundle.getContents()用法及代碼示例


描述

這個java.util.ListResourceBundle.getContents()方法返回一個數組,其中每個項目都是 Object 數組中的一對對象。每對的第一個元素是鍵,它必須是一個字符串,第二個元素是與該鍵關聯的值

聲明

以下是聲明java.util.ListResourceBundle.getContents()方法

protected abstract Object[][] getContents()

參數

NA

返回值

此方法返回表示鍵值對的 Object 數組的數組。

異常

NA

示例

下麵的例子展示了 java.util.ListResourceBundle.getContents() 方法的用法。

package com.tutorialspoint;

import java.util.*;

// create a class that extends to ListResourceBundle
class MyResources extends ListResourceBundle {

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"hello", "Hello World!"},
         {"bye", "Goodbye World!"},
         {"goodnight", "Goodnight World!"}
      };
   }
}

public class ListResourceBundleDemo {
   public static void main(String[] args) {

      // create a new MyResources instance
      MyResources mr = new MyResources();

      // print the string for key hello
      System.out.println("" + mr.getString("hello"));
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Hello World!

相關用法


注:本文由純淨天空篩選整理自 Java.util.ListResourceBundle.getContents() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。