MessageFormat理解,MessageFormat.format(Object obj)方法
MessageFormat.format(Object obj)方法主要用途为拼接message信息
用法:
Object[] testArgs = {new String("张三"),new String("大傻子")};
MessageFormat form = new MessageFormat("{0}是个{1}");
String format = form.format(testArgs);
System.out.println(format);
输出结果:
张三是个大傻子
疑问一:format(testArgs);这里传的参数为Object[]类型
源码为:
/**
* Formats an object to produce a string. This is equivalent to
* <blockquote>
* {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
* new StringBuffer(), new FieldPosition(0)).toString();</code>
* </blockquote>
*
* @param obj The object to format
* @return Formatted string.
* @exception IllegalArgumentException if the Format cannot format the given
* object
*/
public final String format (Object obj) {
return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
}
这里要求参数为Object类型,尝试传Object类型,代码如下:
public static void main(String[] args) {
//Object[] testArgs = {new String("张三"),new String("大傻子")};
Object testArgs1 = new String("张三");
MessageFormat form = new MessageFormat("{0}是个大傻子");
String format = form.format(testArgs1);
System.out.println(format);
}
报错<java.lang.ClassCastException>信息如下
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at java.text.MessageFormat.format(MessageFormat.java:865)
at java.text.Format.format(Format.java:157)
at lijian.test.Test000.main(Test000.java:14)
深入源码分析:
Format中的format (Object obj)实际return的为
format(obj, new StringBuffer(), new FieldPosition(0)).toString();
找到这个方法
/**
* Formats an object and appends the resulting text to a given string
* buffer.
* If the <code>pos</code> argument identifies a field used by the format,
* then its indices are set to the beginning and end of the first such
* field encountered.
*
* @param obj The object to format
* @param toAppendTo where the text is to be appended
* @param pos A <code>FieldPosition</code> identifying a field
* in the formatted text
* @return the string buffer passed in as <code>toAppendTo</code>,
* with formatted text appended
* @exception NullPointerException if <code>toAppendTo</code> or
* <code>pos</code> is null
* @exception IllegalArgumentException if the Format cannot format the given
* object
*/
public abstract StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos);
是一个抽象方法,由于是MessageFormat调用的,所以在MessageFormat类中找其实现方法
// Overrides
/**
* Formats an array of objects and appends the <code>MessageFormat</code>'s
* pattern, with format elements replaced by the formatted objects, to the
* provided <code>StringBuffer</code>.
* This is equivalent to
* <blockquote>
* <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code>
* </blockquote>
*
* @param arguments an array of objects to be formatted and substituted.
* @param result where text is appended.
* @param pos On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @exception IllegalArgumentException if an argument in the
* <code>arguments</code> array is not of the type
* expected by the format element(s) that use it.
*/
public final StringBuffer format(Object arguments, StringBuffer result,
FieldPosition pos)
{
return subformat((Object[]) arguments, result, pos, null);
}
这里对arguments参数做了一个强转...强转为了Object[]方法,怪不得报错了
由于业务需要,这里必须传Object,而不能传数组,这如何解决?
掩耳盗铃方案:
public static void main(String[] args) {
Object[] testArgs = {new String("张三"),new String("大傻子")};
Object testArgs1 = testArgs;
MessageFormat form = new MessageFormat("{0}是个{1}");
String format = form.format(testArgs1);
System.out.println(format);
}
将Object[]数组转为Object类型,谁让它是根类呢,啥都能装
到MessageFormat类中的format方法时强转回Object[]数组,就不会报错了
运行结果:
你是个大傻子
MessageFormat理解,MessageFormat.format(Object obj)方法的更多相关文章
- String.format(String format, Object... args)方法详解
很多次见到同事使用这个方法,同时看到https://blog.csdn.net/qq_27298687/article/details/68921934这位仁兄写的非常仔细,我也记录一下,好加深印象. ...
- Java面试中hashCode()与equals(Object obj)方法关系的准确回答
原文地址: https://blog.csdn.net/qq_19260029/article/details/77917925 hashCode()与equals(Object obj)都是Java ...
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法
在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...
- Class.isAssignableFrom(Class clz)与instanceof与Class.isInstance(Object obj) 的区别和联系
编程的时候可能会遇到一个不知道它属于哪个类的对象,我们可以用下列运算符或者方法来判断. 1.instanceof instanceof是运算符只被用于对象引用变量,检查左边的被测试对象是不是右边类或 ...
- String.format(String format,Object... args)的用法
String.format(String format, Object... args)方法详解 以前也看到过很多次这个用法,一直记不牢靠,今天整理一下. 我仅仅举几个例子稍做说明: String ...
- instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系 编程的时候可能会遇到一个不知道它属于哪个类的 ...
- File类--System.out.print(Object obj)的理解
一.File 类(java.io) 概述:Java中使用File类来表示文件或者文件夹对象! 抽象路径名:描述文件或文件夹时,使用的路径符号,就是一个对象的字符串表示形式,如"c:\ ...
- ArrayList集合--关于System.out.print(Object obj);的理解
1.ArrayList集合中常用的方法 ArrayList<Student> stuArrayList = new ArrayList<>(); //定义一个集合对象 stuA ...
- 理解JAVA - 面向对象(object) - 属性,方法
理解JAVA - 面向对象(object) - 属性,方法 多态的体现: 向上造型,父类接收子类对象:向上造型: 从父类角度看不到子类独有的方法:面向对象,人类认知世界的方式:生活中每天都 ...
随机推荐
- MySQL 目录结构信息
bin 目录,存储可执行文件. data 目录,存储数据文件. docs 目录,文档. include 目录,存储包含的头文件. lib 目录,存储库文件. share 目录,错误信息和字符集文件.
- js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd
昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...
- boxworld开发日记2019-6-8
打算做一个类似RimWorld的游戏,这里记录一下历程.首先,简单回顾一下. 2018年12月23日 场景管理,打算使用四叉树,后来发现四叉树在空间组织和内存占用方面并不占优势,之后计划使用地图分块 ...
- liunx 用户切换 su sudo
liunx 用户操作#useradd test#passwd test 用户身份切换su 切换用户 需要知道切换用户的密码1.su [-lm] [-c命令] [username] #su -login ...
- [Qt Creator 快速入门] 第3章 窗口部件
从这一章开始正式接触Qt的窗口部件.在第2章曾看到 Qt Creator 提供的默认基类只有 QMainWindow.QWidget 和 QDialog 这3种.QMainWindow 是带有菜单栏和 ...
- 51nod2006 飞行员配对(二分图最大匹配)
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- 如何在Eclipse或者Myeclipse中使用tomcat(配置tomcat,发布web项目)?(图文详解)(很实用)
前期博客 Eclipse里的Java EE视图在哪里?MyEclipse里的Java EE视图在哪里?MyEclipse里的MyEclipse Java Enterprise视图在哪里?(图文详解) ...
- C#学习-EF在三层中使用
1.搭建普通三层 DAL层,BLL层,Model层,Web层: DAL层引用Model层 BLL层引用DAL层和Model层 Web层引用BLL层和Model层 2.实现EF三层的搭建(添加引用,修改 ...
- Java高级程序员(5年左右)面试的题目集
Java高级程序员(5年左右)面试的题目集 https://blog.csdn.net/fangqun663775/article/details/73614850?utm_source=blogxg ...