利用Java反射机制,调用一个类的构造函数和一般函数
2010-09-17 22:58
751 查看
【前言】看了侯捷大师的文档,果然很有收获,嘿嘿(MT总是这样笑。。。。)
用于测试的TestClass:
运行结果:
Invoke the default ctor:
<1> <somebody>
********************************
Invoke the ctor with parameters:
<2> <anybody>
********************************
Invoke a function of TestClass, eg setName:
<1> <BaoBao>
package test; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import test2.TestClass; /* * 通过反射调用TestClass的构造函数和一般函数 */ public class Reflection2 { public static void main(String[] args) { /* * 在Reflection 动态机制中有两种作法,一个针对“无自变量ctor”, 一个针对“带参数ctor”。 */ System.out.println("Invoke the default ctor:"); try { Class testClass1 = Class.forName("test2.TestClass"); // 调用缺省构造函数,直接用testClass调用就可 Object obj = testClass1.newInstance(); System.out.println(obj); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("********************************"); System.out.println("Invoke the ctor with parameters:"); try { Class testClass2 = Class.forName("test2.TestClass"); // 首先准备一个Class[]做为ctor的参数类型(本例指定为一个int和一个string), Class[] pType = new Class[] { int.class, Class.forName("java.lang.String") }; // 调用pType为变量的getConstructor(),获得一个专属ctor Constructor ctor = testClass2.getConstructor(pType); // 准备一个Object[] 做为ctor实参值 Object[] obj = new Object[] { 2, "anybody" }; // 调用上述专属ctor的newInstance() Object result = ctor.newInstance(obj); System.out.println(result); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("********************************"); System.out.println("Invoke a function of TestClass, eg setName:"); try { Class testClass3 = Class.forName("test2.TestClass"); //准备一个Class[]做为调用函数的参数类型(本例指定为String) //如果参数多个一个,需要声明为Class[] Class pTypes = Class.forName("java.lang.String"); //以pTypes为自变量调用getMethod(),获得特定的Method object //在getMethod中指定了要调用的函数名称setName和参数类型pTypes Method method = testClass3.getMethod("setName", pTypes); //准备一个Object放置自变量,或者直接声明为String aName = new String("Bao"); Object aName = "BaoBao"; //这里声明一个TestClass的对象,是因为setName需要修改一个对象的数据成员 //如果没有这个对象的话,setName中将修改谁的name呢? //aStudent是id为1,name为somebody的默认值 TestClass aStudent = new TestClass(); //用上述所得之特定Method object的invoke(),就是TestClass中的setName() method.invoke(aStudent, aName); System.out.println(aStudent); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
用于测试的TestClass:
package test2; import java.util.*; public class TestClass implements Comparable { private int id; private String name; public TestClass() { this.id = 1; this.name = "somebody"; } public TestClass(int id, String name) { this.id = id; this.name = name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public int getId(){ return this.id; } public int compareTo(Object obj) { return 0; } public String toString() { return "<"+ id + "> "+"<"+name+">"; } }
运行结果:
Invoke the default ctor:
<1> <somebody>
********************************
Invoke the ctor with parameters:
<2> <anybody>
********************************
Invoke a function of TestClass, eg setName:
<1> <BaoBao>
相关文章推荐
- 一个将类的构造函数调用识别成函数指针的问题
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- 利用VC中函数调用一个.exe文件
- 在vc中调用一般的.exe只需要一个函数
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- C语言创建一个数组,利用函数调用实现数组的初始化,逆置和清零
- 利用闭包返回一个计数器函数,每次调用它返回递增整数:
- 牛客网Java刷题知识点之构造函数可以调用一般函数,但是一般函数不可以直接调用构造函数
- 编写一个与3.12节中dup2功能相同的函数,要求不调用fcntl函数,并且要有正确的出错处理(APUE)
- 为什么super()和this()调用语句不能同时在一个构造函数中出现的解释
- new 一个函数和直接调用函数的异同
- 如何在 KEIL C51(v6.21) 中调用汇编函数的一个示例
- OpenResty如何在一个lua文件调用另一个lua文件的函数
- Java反射机制demo(五)—获得并调用一个类中的方法
- 利用JNI技术在Android中调用C++形式的OpenGL ES 2.0函数
- python 一个.py文件如何调用另一个.py文件中的类和函数
- 在服务中、一般程序中利用 Inet 相关函数下载数据
- 一个编译错误引发的对default构造函数的调用时机的探讨
- 利用JAVA反射机制实现调用私有方法
- C++中构造函数和析构函数避免调用虚函数的问题