黑马程序员技术交流社区
标题:
数组强转问题
[打印本页]
作者:
msileandroid
时间:
2013-6-13 10:41
标题:
数组强转问题
本帖最后由 孙百鑫 于 2013-6-22 01:03 编辑
<p style="margin-top: 4.32pt; margin-bottom: 0pt; direction: ltr; unicode-bidi: embed; vertical-align: baseline;"><span style="font-size:12.0pt;font-family:" times="" new="" roman";="" mso-ascii-font-family:"times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:="" +mn-cs;mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:="" text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">Class
clazz = Class.forName(arg[0]);</span></p>
<p style="margin-top: 4.32pt; margin-bottom: 0pt; direction: ltr; unicode-bidi: embed; vertical-align: baseline;"><span style="font-size:12.0pt;font-family:" times="" new="" roman";="" mso-ascii-font-family:"times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:="" +mn-cs;mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:="" text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">Method
mMain = clazz.getMethod("main", String[].</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";mso-ascii-font-family:="" "times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;="" mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">class</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">);</span></p>
<p style="margin-top: 4.32pt; margin-bottom: 0pt; direction: ltr; unicode-bidi: embed; vertical-align: baseline;"><span style="font-size:12.0pt;font-family:" times="" new="" roman";="" mso-ascii-font-family:"times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:="" +mn-cs;mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:="" text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">mMain.invoke(</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";mso-ascii-font-family:="" "times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;="" mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">null</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">,</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";mso-ascii-font-family:="" "times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;="" mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">new</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">
Object[]{</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";="" mso-ascii-font-family:"times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:="" +mn-cs;mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">new</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">
String[]{"aaa","bbb"}});</span></p>
<p style="margin-top: 4.32pt; margin-bottom: 0pt; direction: ltr; unicode-bidi: embed; vertical-align: baseline;"><span style="font-size:12.0pt;font-family:" times="" new="" roman";="" mso-ascii-font-family:"times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:="" +mn-cs;mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:="" text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">mMain.invoke(</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";mso-ascii-font-family:="" "times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;="" mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">null</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">,(Object)</span><span style="font-size:12.0pt;font-family:" times="" new="" roman";mso-ascii-font-family:="" "times="" roman";mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;="" mso-bidi-theme-font:minor-bidi;color:black;mso-color-index:1;mso-font-kerning:="" 12.0pt;language:en-us;font-weight:bold;mso-style-textfill-type:solid;="" mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;="" mso-style-textfill-fill-alpha:100.0%"="">new</span><span style="font-size:12.0pt;
font-family:" times="" new="" roman";mso-ascii-font-family:"times="" roman";="" mso-fareast-font-family:宋体;mso-bidi-font-family:+mn-cs;mso-bidi-theme-font:="" minor-bidi;color:black;mso-color-index:1;mso-font-kerning:12.0pt;language:en-us;="" mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;="" mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%"="">
String[]{"aaa","bbb"});</span></p>
复制代码
如何理解后两句的参数?
作者:
msileandroid
时间:
2013-6-13 10:47
Class clazz = Class.forName(arg[0]); Method mMain = clazz.getMethod("main", String[].class); mMain.invoke(null,new Object[]{new String[]{"aaa","bbb"}}); mMain.invoke(null,(Object)new String[]{"aaa","bbb"})
作者:
msileandroid
时间:
2013-6-13 10:49
不好意思刚卡死了,就上面那后两句
作者:
′х.|鰉❤耔
时间:
2013-6-13 18:20
Class clazz = Class.forName(arg[0]); Method mMain = clazz.getMethod("main", String[].class); mMain.invoke(null,new Object[]{new String[]{"aaa","bbb"}}); mMain.invoke(null,(Object)new String[]{"aaa","bbb"})
楼主你这个是通过反射的方法来获取对象的方法,
我记得视屏中有讲过这个,new Object[]{new String[]{"aaa","bbb"}}和(Object)new String[]{"aaa","bbb"}其实是一样的效果,但是前者要先进行解包的过程而后者就不需要了
作者:
孙百鑫
时间:
2013-6-22 01:03
楼主您好~帖子长时间未作出回答,我已经将您的帖子改成已解决。如果有问题的话可以私密我哦~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2