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.