Java ResourceBundle setParent() 方法



描述

Java ResourceBundle setParent(ResourceBundle parent) 方法設定此捆綁包的父捆綁包。當此捆綁包不包含特定資源時,getObject 會搜尋父捆綁包。

宣告

以下是 java.util.ResourceBundle.setParent() 方法的宣告

protected void setParent(ResourceBundle parent)

引數

parent − 此捆綁包的父捆綁包。

返回值

此方法不返回值。

異常

在 US 本地化資源捆綁包中設定父捆綁包示例

以下示例演示了 Java ResourceBundle setParent() 方法的用法。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 setParent() 方法,並設定了超級父級。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Hello World!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Hello World!");
      return key;
   }

   @Override
   protected void setParent(ResourceBundle parent) {
      super.setParent(parent);
   }

   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      // print the string array assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));
   }
}

輸出

假設在你的 CLASSPATH 中有一個名為 hello_en_US.properties 的資原始檔,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Hello World!

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

Hello World!

在法國本地化資源捆綁包中設定父捆綁包示例

以下示例演示了 Java ResourceBundle setParent() 方法的用法。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 setParent() 方法,並設定了超級父級。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Bonjour le monde!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Bonjour le monde!");
      return key;
   }

   @Override
   protected void setParent(ResourceBundle parent) {
      super.setParent(parent);
   }

   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE);

      // print the string array assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));
   }
}

輸出

假設在你的 CLASSPATH 中有一個名為 hello_fr_FR.properties 的資原始檔,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Bonjour le monde!

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

Bonjour le monde!

在德國本地化資源捆綁包中設定父捆綁包示例

以下示例演示了 Java ResourceBundle setParent() 方法的用法。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 setParent() 方法,並設定了超級父級。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Hallo Welt!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Hallo Welt!");
      return key;
   }

   @Override
   protected void setParent(ResourceBundle parent) {
      super.setParent(parent);
   }

   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMANY);

      // print the string array assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));
   }
}

輸出

假設在你的 CLASSPATH 中有一個名為 hello_de_DE.properties 的資原始檔,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Hallo Welt!

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

Hallo Welt!
java_util_resourcebundle.htm
廣告

© . All rights reserved.