练习java文档ResourceBundle

练习java文档ResourceBundle,第1张

练习java文档ResourceBundle

正式练习ResourceBundle
变量
parent 是protected访问控制,无法访问
构造
ResourceBundle() 因为是抽象类,无法实例化,也就无法使用构造器
方法
clearCache() 不懂什么意思,也不知道怎么用。还有一个重载的方法,涉及到反射的知识,先放着我懂了,如果不clearCache()的话,再创建一个bundle,getString得到的值和之前getString得到的是同一个对象,如果clearCache()后,再创建一个bundle,getString()得到的则不是同一个对象。估计是bundle的创建会先从缓冲里查看是不是已经载入过了,如果载入过了直接复用。clearCache()则是清理了缓冲里的资源,再创建一个bundle的话,只能重新载入,这个时候就和之前的String不会是同一个对象了。

ResourceBundle bundle = ResourceBundle.getBundle("mess");
		String a = bundle.getString("hello");
		bundle = ResourceBundle.getBundle("mess");
		String b = bundle.getString("hello");
		System.out.println(a == b);
		ResourceBundle.clearCache();
		bundle = ResourceBundle.getBundle("mess");
		String c = bundle.getString("hello");
		System.out.println(a == c);


containsKey() 是否包含某个键,
getbaseBundleName() 以mess_zh_CN.properties为例子,返回mess
getBundle() 有的方法涉及到Module类和ClassLoader类,没搞懂。还有加了个Control有什么用?没搞出来区别。
getKeys()
getLocale()
getObject()
getString()
getStringArray()
handleGetObject() 必须要用子类继承重写,才有用
handleKeySet() 和上面一样,都是用protected修饰的。
keySet() 返回键的set
setParent() 也是protected修饰,不知道怎么重写?。

import java.util.*;

import static java.util.ResourceBundle.*;

public class Test
{
	public static void main(String[] args) throws Exception,Throwable
	{
		//ResourceBundle
		//变量
		//System.out.println(ResourceBundle.parent);
		//构造器
		//ResourceBundle rb = new ResourceBundle();

		ResourceBundle bundle = ResourceBundle.getBundle("mess");
		String a = bundle.getString("hello");
		bundle = ResourceBundle.getBundle("mess");
		String b = bundle.getString("hello");
		System.out.println(a == b);
		ResourceBundle.clearCache();
		bundle = ResourceBundle.getBundle("mess");
		String c = bundle.getString("hello");
		System.out.println(a == c);

		System.out.println(bundle.containsKey("hello"));
		System.out.println(bundle.containsKey("abc"));

		//System.out.println(bundle.getString("abc"));
		System.out.println(bundle.getString("hello"));
		System.out.println(bundle.getbaseBundleName());

		bundle = ResourceBundle.getBundle("mess");
		System.out.println(bundle.getString("hello"));
		bundle = ResourceBundle.getBundle("mess",new Locale("en","US"));
		System.out.println(bundle.getString("hello"));

		bundle = ResourceBundle.getBundle("mess",Locale.ENGLISH,Control.getControl(Control.FORMAT_PROPERTIES));
		System.out.println(bundle.getString("hello"));
		bundle = ResourceBundle.getBundle("mess",Locale.ENGLISH,Control.getControl(Control.FORMAT_CLASS));
		System.out.println(bundle.getString("hello"));

		bundle = ResourceBundle.getBundle("mess",Control.getControl(Control.FORMAT_DEFAULT));
		System.out.println(bundle.getString("hello"));

		bundle = ResourceBundle.getBundle("mess",Control.getControl(Control.FORMAT_DEFAULT));
		Iterator iterator = bundle.getKeys().asIterator();
		while(iterator.hasNext())
		{
			System.out.println(iterator.next());
		}

		System.out.println(bundle.getLocale());

		System.out.println(bundle.getObject("abc"));
		System.out.println(Arrays.toString((String[])bundle.getObject("uuu")));
			
		//System.out.println(bundle.handleGetObject("hello"));		


		MyResources resource = new MyResources();
		System.out.println(resource.handleGetObject("good"));

		System.out.println(resource.handleKeySet());

		System.out.println(bundle.keySet());
		resource.setParent(bundle);
		//System.out.println(resource.getObject("hello"));
	}	
}

class MyResources extends ResourceBundle
{
	public Object handleGetObject(String key)
	{
		if(key.equals("good")) return "非常好";
		if(key.equals("bad")) return "不太好";
		return null;
	}
	public Enumeration getKeys()
	{
		return Collections.enumeration(keySet());
	}
	protected Set handleKeySet()
	{
		return new HashSet(Arrays.asList("good","bad"));
	}
	protected void setParent(ResourceBundle parent)
	{

	}
	
}
		

mess_zh_CN.java

import java.util.*;
public class mess_zh_CN extends ListResourceBundle
{
	private final Object myData[][]=
	{
		{"hello","类里的Hello!"},
		{"abc", new Dog()},
		{"uuu", new String[]{"a","b","c"}}
	};
	public Object[][] getContents()
	{
		return myData;
	}
}
class Dog
{
	String name = "小狗";
	
	public String toString()
	{
		return name;
	}
}

mess_zh_CN.properties

hello=你好!
hello=777

mess_en_US.properties

hello=Welcom You!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5693199.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存