疯狂java讲义第七章补充习题第18题答案(MessageFormat)

18、使用MessageFormat处理包含占位符
的字符串
书上练习,如果传入数字,比如2022,会显示"2,022"
MessageFormat的java文档练习
如果有两个{0} 那么都会被转换,如果没有{0}还有参数的话,就正常显示文本。如果没有参数则是一个实例方法。
如果有{1} 但是没有传入对应的参数,则输出就是{1}
单引号有特殊的作用,能够将包裹的文本不进行特殊的解释
不知道SubformatPattern应该怎么写

Object[] args = {1,3,4};
args = {1,2} 这样会报错 但是这么写就没问题 args = new Object[] {1,2}
在这里插入图片描述
在这里插入图片描述
不知道最后一个什么意思,大于1?但是如果写成2#q其实也是相同的效果。
下面这个不理解
在这里插入图片描述
还有parse不理解:其实就是按照格式解析,有点类似Split()不过是按照{0},{1},来更灵活的解析。也需要new ParsePosition()来定位是{0}还是{1}之类的。返回的是Object[]

内部类
Field
Field的构造器是protected控制的,无法new
readResolve()也是protected修饰的,无法使用。

正式练习MessageFormat
构造器
MessageFormat() 其中一个,有指定Locale,但是我不知道在什么地方应用。。
方法
applyPattern() 就是将MessageFromat的格式转换成另一个,覆盖之前的那一个,也就是像介绍里说的那样,复用format,不过增加了修改模板的功能。
clone(),浅层克隆,因为equals重写过,只要pattern相同返回true,= = 却是返回false,所以不是同一个对象。
equals() 只要pattern相同就返回true
format()涉及到FieldPosition类,还有,返回值为什么要加上final,(final StringBuffer ,我懂了,是final方法,不可以被重写的方法。我用javadoc对自己写的一个final方法生成文档 javadoc -encoding utf-8 -d finaldoc Test.java
结果是并没有向像java文档一样显示final.。那我就不知道是不是final方法了。继承试一下,而是显示如下:
在这里插入图片描述
是的,没错,就是final方法
在这里插入图片描述
不知道这个FieldPosition怎么用
formatToCharacterIterator() 返回的是MessageFormat的Field这个类。不知道怎么用
Object也代表数组String[] 也是Object,我会误以为需要声明Object[]。然后这里面的方法也要看,也是类的方法
在这里插入图片描述

补充AttributedCharactorIterator
getAllAttributeKeys() 不懂是干嘛的
getAttribute() 不知道干嘛用的
getAttributes() 不知道那个属性是干嘛的
getRunLimit() 不知道RunLimit是什么意思,有参数那2个也不知道要怎么用
getRunStart() 和上面getRunLimit()的问题差不多
补充AttributedCharacterIterator.Attribute
变量都不明白什么意思
INPUT_METHOD_SEGMENT
LANGUAGE
READING
构造器是protected的,无法new
equals()猜测是意思相同即可,不需要是同一个对象。由于无法new。。我也不知道怎么创建一个Attribute
getName() protected修饰
hashCode()
readResolve() protected修饰
toString()

//回到MessageFormat
getFormats() 不理解什么意思,,还涉及到Format类 , 为什么返回的是null,因为我使用了Arrays.toString()包装, 但是里面的是一个类,不知道为什么返回null,不知道为什么,返回的引用类型有一个,但是却是一个空指针。 需要这样设置才会有Format
{0,number} {1,date}

补充Format
内部类Field先放着
构造器是protected修饰的,无法new
方法
因为返回的是空指针,所以方法也先放着

//回到MessageFormat
getLocale()
hashCode()
parse() 中的ParsePosition,的意思是查询开始的位置,类似下面这个解释。应该还有其他效果,不过我不知道而已
在这里插入图片描述
下面这4点看不懂
在这里插入图片描述
parseObject() 看不懂,不会用
setFormat() 涉及到Format,设置{0,number} 后面的number类型 ,如果类似下面的例子{0,date} {0,number} {0,date} ,如果用setFormat()的话,index为0,就改变第一个{0,date} , 为1的话,就改变第二个{0,number},2的话就该百年第3个{0,date};
setFormatByArgumentIndex() 而这个方法,对于上面的例子,index为0的话,会把3个{0,…}都改变。
setFormats() 和setFormat()很像,不过是靠位序定位
setFormatsByArgument() 和setFormatByArgument()很像,不过是靠位序定位
例子

MessageFormat form2 = new MessageFormat("{0,number}, {0,date}{1,date}, {2, choice, 0#a|1#b|2#c}");
		System.out.println(Arrays.toString(form2.getFormats()));

		form2.setFormat(0,new ChoiceFormat("0#ab|1#cd"));
		System.out.println(Arrays.toString(form2.getFormats()));

		form2.setFormatByArgumentIndex(0,NumberFormat.getInstance());
		System.out.println(Arrays.toString(form2.getFormats()));

		MessageFormat form3 = new MessageFormat("{0,number}, {0,number},{0,number}");
		Format[] formats = new Format[]{new ChoiceFormat("0#a|1#b"),null,null};
		form3.setFormats(formats);
		System.out.println(Arrays.toString(form3.getFormats()));
		form3.setFormatsByArgumentIndex(formats);
		System.out.println(Arrays.toString(form3.getFormats()));

setLocale()
toPattern()

import java.text.*;
import java.util.*;
import java.time.*;

import static java.text.MessageFormat.*;
import static java.text.AttributedCharacterIterator.*;

public class Test
{
	public static void main(String[] args) throws Exception,Throwable
	{
		//MessageFormat
		//书上练习
		String msg = "{0,date}年后,世界{1},火箭{2}。";
		System.out.println(MessageFormat.format(msg,new Date(),"和平","升空")); 

		//java文档练习
		System.out.println(MessageFormat.format("{0,date,short},这是日期。",new Date()));
		System.out.println(MessageFormat.format("{0,time,full},这是时间。",new Date()));
		System.out.println(MessageFormat.format("'{0}', {0}","aa"));
		System.out.println(MessageFormat.format("{0} , {0}","aa"));
		System.out.println(MessageFormat.format("0, 1","bb"));
		System.out.println(MessageFormat.format("'{''}'","aa"));
		System.out.println(MessageFormat.format("{0,number,$'#',##}", 3145));
		System.out.println(MessageFormat.format("'{0}",22));
		System.out.println(MessageFormat.format("{0}, {1},{10}","22",1,2,3,4,5,6,7,8,9,10));

		//一次性的使用
		int planet = 7;
		String event = "a disturbance in the Force";
		String result = MessageFormat.format(
			"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
			planet, new Date(), event);
		System.out.println(result);

		//可以复用的
		int fileCount = 1273;
		String diskName = "MyDisk";
		Object[] testArgs = {new Long(fileCount), diskName};
		MessageFormat form = new MessageFormat(
			"The disk \"{1}\" contains {0} file(s).");
		System.out.println(form.format(testArgs));

		//使用ChoiceFormat为单复数生成正确的形式
		form = new MessageFormat("磁盘\"{1}\"包含{0}。");
		double[] filelimits = {0,1,2};
		String[] filepart = {"no files","one file","{0,number} files"};
		ChoiceFormat fileform = new ChoiceFormat(filelimits,filepart);
		form.setFormatByArgumentIndex(0,fileform);
		
		fileCount =1;
		diskName = "MyDisk";
		Object[] testArgs2 = {new Long(fileCount),diskName};
		System.out.println(form.format(testArgs2));

		//自己用一下ChoiceFormat写一个例子
		form = new MessageFormat("我是{0},因为我很{1}。");
		double[] numbers = {0,1,2};
		String[] roles = {"超人","琦玉","杰斯"};
		String[] descs = {"厉害","无敌","正义"};
		ChoiceFormat fileform2 = new ChoiceFormat(numbers,roles);
		form.setFormatByArgumentIndex(0,fileform2);
		
		int number =2;
		Object[] testArgs3 = {new Long(number),descs[number]};
		System.out.println(form.format(testArgs3));

		//再写一遍
		MessageFormat form2 = new MessageFormat("飞向{0},追寻{1}");
		double[] numbers2 = {0,1,2};
		String[] descs2 = {"天堂","美女","未来"};
		ChoiceFormat choices= new ChoiceFormat(numbers2,descs2);
		form2.setFormatByArgumentIndex(0,choices);
		
		int num = 1;
		Object[] args3 = {num, "幸福"};
		System.out.println(form2.format(args3));

		//再写一遍
		MessageFormat form3 = new MessageFormat("{0}在我面前,我很{1}。");
		double[] nums = {0,1,2};
		String[] whos = {"胡萦月","杨雨婷","甜甜"};
		ChoiceFormat format4 = new ChoiceFormat(nums,whos);
		form3.setFormatByArgumentIndex(0,format4);
		int num3 = 0;
		Object[] args4 = {num3,"开心"};
		System.out.println(form3.format(args4));

		//再写一遍
		MessageFormat format5 = new MessageFormat("是啊,{0}到底喜欢谁呢?答案:{1}。");
		double[] nums5 = {0,1,2};
		String[] names = {"妖怪吧","李毛睿","程序代号"};
		ChoiceFormat form5 = new ChoiceFormat(nums5, names);
		format5.setFormatByArgumentIndex(0,form5);
		int who=0;
		Object[] args5 = {who,"谁也不爱"};
		System.out.println(format5.format(args5));
		Object[] args6 = {1,"胡萦月"};
		System.out.println(format5.format(args6));
		Object[] args7 = {2,"创造"};
		System.out.println(format5.format(args7));

		MessageFormat form6 = new MessageFormat("");
		form6.applyPattern(
			"There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
		System.out.println(form6.format(new Object[]{0}));
		
		MessageFormat form7 = new MessageFormat("");
		form7.applyPattern("我估计我是{0,choice,0#缺少交流|1#缺爱|2#想谈恋爱了}");
		System.out.println(form7.format(new Object[]{5}));

		//提到了Parse
		MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
		Object[] objs = {new Double(3.1415)};
		result = mf.format(objs);
		objs = mf.parse(result,new ParsePosition(0));
		System.out.println(Arrays.toString(objs));

		mf = new MessageFormat("{0},{0},{0}");
		String forParsing = "x,y,z";
		objs = mf.parse(forParsing,new ParsePosition(0));
		System.out.println(Arrays.toString(objs));

		//Field内部类
		System.out.println(Field.ARGUMENT);
		//System.out.println(new Field("abc"));

		//构造器
		MessageFormat msgform = new MessageFormat("我是{0}。");
		System.out.println(msgform.format(new Object[]{"侠客"}));
		MessageFormat msgform2 = new MessageFormat("驰骋{0,number,#.##}。",Locale.ENGLISH);
		System.out.println(msgform2.format(new Object[]{12.3456}));
		//方法
		MessageFormat mform = new MessageFormat("小狗,叮当,{0}");
		System.out.println(mform.format(new Object[] {"小(>^ω^<)喵"}));
		mform.applyPattern("漂亮的姑娘是{0}");
		System.out.println(mform.format(new Object[]{"阿斯蒂芬"}));
		
		Object mform1 = mform.clone();
		System.out.println(mform==mform1);
		((MessageFormat)mform1).applyPattern("");
		System.out.println(mform.equals(mform1));

		mform = new MessageFormat("超级{0}人{1}");
		StringBuffer sb = new StringBuffer("逗狗");
		FieldPosition fp = new FieldPosition(1);
		System.out.println(fp);
		sb = mform.format(new Object[]{"赛亚","afd"},sb,fp);
		System.out.println(sb);
		System.out.println(fp);

		sb = mform.format(new Object[]{"无敌","琦玉"},sb,fp);
		System.out.println(sb);
		
		System.out.println(MessageFormat.format("我是{0},目标向{1}进军。","修仙者","仙界"));
		
		form = new MessageFormat("I like {0}.");
		AttributedCharacterIterator iterator = form.formatToCharacterIterator(new String[]{"cat"});
		System.out.println(iterator.first());
		System.out.println(iterator.next());
		System.out.println(iterator.next());
		System.out.println(iterator.next());
		System.out.println(iterator.next());
		System.out.println(iterator.getRunLimit());
		System.out.println(iterator.getRunStart());

		form = new MessageFormat("who like{1} me? {0}");
		AttributedCharacterIterator iterator2 = form.formatToCharacterIterator(new String[]{"cat"});
		System.out.println(iterator2.getAllAttributeKeys());
		Set<AttributedCharacterIterator.Attribute> set = iterator2.getAllAttributeKeys();
		System.out.println(set);
		Iterator it = set.iterator();
		//System.out.println(it.next());
		AttributedCharacterIterator.Attribute attri = (AttributedCharacterIterator.Attribute)it.next();
		System.out.println(iterator2.getAttribute(attri));
		System.out.println(iterator2.getAttributes());
		System.out.println(iterator2.getRunLimit());
		System.out.println(iterator2.getRunLimit(attri));
		System.out.println(iterator2.getRunLimit(set));
		System.out.println(iterator2.getRunStart());
		System.out.println(iterator2.getRunStart(attri));
		System.out.println(iterator2.getRunStart(set));

		//AttributedCharacterIterator.Attribute
		System.out.println(Attribute.INPUT_METHOD_SEGMENT);
		System.out.println(Attribute.LANGUAGE);
		System.out.println(Attribute.READING);
		System.out.println(attri.equals(attri));
		//System.out.println(attri.getName());
		System.out.println(attri.hashCode());
		System.out.println(attri.toString());

		form = new MessageFormat("我爱{0}!");
		form.applyPattern("我第二爱{0}!");
		System.out.println(form.format(new String[]{"胡银月"}));
		System.out.println(form.getFormats()+":"+form.getFormats().length);
		System.out.println(form.getFormatsByArgumentIndex()+":"+form.getFormatsByArgumentIndex().length);

		//补充Format
		/*
		Format fo = form.getFormats()[0];
		System.out.println(fo);
		Format fo2 = (Format)fo.clone();
		System.out.println(fo.equals(fo2)+":"+(fo == fo2));
		*/
		System.out.println(form.getLocale());
		System.out.println(form.hashCode());

		form = new MessageFormat("你真是一个{0}的{1}。");
		System.out.println(Arrays.toString(form.parse("你真是一个漂亮又大方的女人。")));
		System.out.println(Arrays.toString(form.parse(" dogdog 你真是一个漂亮又大方的女人。",new ParsePosition(7))));
		
		form = new MessageFormat("信封是{0},我估计是{1}发来的。");
		ParsePosition pp = new ParsePosition(0);
		System.out.println(Arrays.toString(form.parse("信封是红色的,我估计是小刘发来1的。啊",pp)));
		System.out.println(pp.getErrorIndex());

		form = new MessageFormat("今天是{0,date,full}。");
		ParsePosition pp2 = new ParsePosition(0);
		System.out.println(form.format(new Object[]{new Date()}));
		System.out.println(form.parseObject("今天是2022年1月3日星期一",pp2));
		
		form.setLocale(Locale.ENGLISH);
		System.out.println(form.format(new Object[]{new Date()}));

		System.out.println(form.toPattern());
	}
	/**
	* 测试一下final方法,的javadoc
	* @return StringBuffer是返回值
	*/
	public final StringBuffer test()
	{
		return new StringBuffer("");
	}
}
class Other extends MessageFormat
{
	public Other(String ab)
	{
		super(ab);
	}	
	/*
	//重写format方法
	public final StringBuffer format(Object[] arguments ,StringBuffer result,FieldPosition pos)
	{

	}
	*/
}

		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流cpu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值