反射学习--对接收数组参数的成员方法进行反射
2014-03-10 21:01
513 查看
public class ClassMain {
/**
*写个程序,能够根据用户提供的类名,去执行该类中的Main方法。
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Method mainmethod=Class.forName(args[0]).getMethod("main", String[].class);//用反射方法得到某类的main方法
//mainmethod.invoke(null, new String[]{"11","22"});//兼容1.4版本,把这个数组拆包,变成三个参数,所以报参数错误。
//mainmethod.invoke(null, new Object[]{new String[]{"11","22"}});//把数组封装到Object数组中,拆包后是一个字符串型的数组。
mainmethod.invoke(null, (Object)new String[]{"11","22"});//把字符串数组标示成一个Object对象,编译器就不会拆包了。
}
}
//数组的反射
int[] a1=new int[3];
int[] a2=new int[4];
int[][] a3=new int[1][2];
String[] a4=new String[3];
System.out.println(a1.getClass()==a2.getClass());//返回true
// System.out.println(a1.getClass()== a3.getClass());//编译器提示错误
// System.out.println(a1.getClass()==a4.getClass());//编译器提示错误
System.out.println(a1.getClass().getSuperclass().getName());//得到数组的父类字节码名称java.lang.Object
Object obj1=a1;
//Object[] obj2=a1;//一维基本类型的数组不能转换成Object类型数组,因基本类型不是Object对象。
/**
*写个程序,能够根据用户提供的类名,去执行该类中的Main方法。
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Method mainmethod=Class.forName(args[0]).getMethod("main", String[].class);//用反射方法得到某类的main方法
//mainmethod.invoke(null, new String[]{"11","22"});//兼容1.4版本,把这个数组拆包,变成三个参数,所以报参数错误。
//mainmethod.invoke(null, new Object[]{new String[]{"11","22"}});//把数组封装到Object数组中,拆包后是一个字符串型的数组。
mainmethod.invoke(null, (Object)new String[]{"11","22"});//把字符串数组标示成一个Object对象,编译器就不会拆包了。
}
}
//数组的反射
int[] a1=new int[3];
int[] a2=new int[4];
int[][] a3=new int[1][2];
String[] a4=new String[3];
System.out.println(a1.getClass()==a2.getClass());//返回true
// System.out.println(a1.getClass()== a3.getClass());//编译器提示错误
// System.out.println(a1.getClass()==a4.getClass());//编译器提示错误
System.out.println(a1.getClass().getSuperclass().getName());//得到数组的父类字节码名称java.lang.Object
Object obj1=a1;
//Object[] obj2=a1;//一维基本类型的数组不能转换成Object类型数组,因基本类型不是Object对象。
相关文章推荐
- 来科普下游标(MSSQL)这东西。。。
- [转载]编程哲理
- [LeetCode] Triangle
- Cmarkup 使用总结 xml解析
- 黑马程序员___构造函数
- 微信平台3G模板制作指南
- (上节)Spring+JDBC+Struts;实现图片上传;配置的属性默认dbc用单例模式,action一定要scope=“prototype”
- Android图片对比源码
- hive 桶相关特性分析
- Mem系列函数与Str系列函数总结(二) memcmp与strcmp/strncmp
- linux学习笔记3:系统命令
- Puppet系列之五:Puppet报告系统Dashboard安装与配置
- java实现堆排序,本人略菜,求指教
- 6182 Ginkgo Numbers
- Playing with ptrace, Part I
- 1048. Inverso
- Django的安装
- Java进击(三)容器类
- Nginx gzip static静态压缩
- linux上未分区硬盘的格式化实践