栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

练习java文档ResourceBundle

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

练习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!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692731.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号