Java ListResourceBundle 的 handleGetObject(String key) 方法



描述

Java ListResourceBundle 的 handleGetObject(String key) 方法從該資源包中獲取給定鍵的物件。如果該資源包不包含給定鍵的物件,則返回null

宣告

以下是java.util.ListResourceBundle.handleGetObject(String key) 方法的宣告

public final Object handleGetObject(String key)

引數

key − 所需物件的鍵

返回值

此方法返回給定鍵的物件,或null

異常

從字串和整數對的 ListResourceBundle 獲取值示例

以下示例演示瞭如何使用 Java ListResourceBundle 的 handleGetObject() 方法從 ListResourceBundle 獲取值。我們建立了一個 ListResourceBundle 物件,其內容是一個由字串和整數對組成的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,並使用 handleGetObject() 方法根據鍵檢索值並將其打印出來。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

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

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"1", 1},
         {"2", 2},
         {"3", 3}
      };
   }
}

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

      // create a new MyResources instance
      MyResources mr = new MyResources();
	  
      // print the value for key 1
      System.out.println("" + mr.handleGetObject("1"));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

1
2
3

從字串對的 ListResourceBundle 獲取值示例

以下示例演示瞭如何使用 Java ListResourceBundle 的 handleGetObject() 方法從 ListResourceBundle 獲取值。我們建立了一個 ListResourceBundle 物件,其內容是一個由字串對組成的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,並使用 handleGetObject() 方法根據鍵檢索值並將其打印出來。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

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

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

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

      // create a new MyResources instance
      MyResources mr = new MyResources();
	  
      // print the value for key 1
      System.out.println("" + mr.handleGetObject("1"));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Hello World!

從字串和物件對的 ListResourceBundle 獲取值示例

以下示例演示瞭如何使用 Java ListResourceBundle 的 handleGetObject() 方法從 ListResourceBundle 獲取值。我們建立了一個 ListResourceBundle 物件,其內容是一個由字串和 Student 物件對組成的二維陣列。然後透過重寫 getContents() 方法添加了一些條目,並使用 handleGetObject() 方法根據鍵檢索值並將其打印出來。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

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

   // get contents must be implemented
   protected Object[][] getContents() {
      return new Object[][] {
         {"1", new Student(1, "Julie")},
         {"2", new Student(2, "Robert")},
         {"3", new Student(3, "Adam")}
      };
   }
}

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

      // create a new MyResources instance
      MyResources mr = new MyResources();
	  
      // print the value for key 1
      System.out.println("" + mr.handleGetObject("1"));
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

[ 1, Julie ]
java_util_listresourcebundle.htm
廣告

© . All rights reserved.