首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

反照机制

2012-08-30 
反射机制public class MethedNamesPrinter {public static void main(String[] args) throws ClassNotFoun

反射机制

public class MethedNamesPrinter {public static void main(String[] args) throws ClassNotFoundException {Class classType = Class.forName("java.lang.String");Method[] methods = classType.getDeclaredMethods();for (Method method : methods) {System.out.println(method.toString());}}}?

//这是一个被访问的类,只有4个构造方法public class ConstructorTest {public ConstructorTest() {System.out.println("默认构造方法被调用");}public ConstructorTest(int arg_1) {System.out.println(arg_1 + "第二个构造方法被调用");}public ConstructorTest(int arg_2, String arg_3) {System.out.println(arg_2 + " " + arg_3 + "第三个构造方法被调用");}private ConstructorTest(String arg_4) {System.out.println(arg_4 + "第四个构造方法被调用,这是私有的");}}//现在想办法让他们都被打印出来//首先打印第一个,默认的构造方法,代码如下Class classType = Class.forName("reflection.ConstructorTest");Constructor constructor = classType.getConstructor(null);constructor.newInstance(null); //getConstructor的参数传null的话就表示没有参数类型,自然选择第一个构造方法//实例constructor在用newInstance()的时候自然不用传参,实例化结果正常打印出//!!!结果是:默认构造方法被调用//现在要访问第二个构造方法,参数是一个int类型,代码如下Class classType = Class.forName("reflection.ConstructorTest");Constructor constructor = classType.getConstructor(new Class[] { int.class });constructor.newInstance(10);//!!!结果是:10第二个构造方法被调用//实际上newInstance()只接受对象,“10”在编译过程自动被包装成Integer了。//访问第三个构造方法,参数列表不同了。如下代码Class classType = Class.forName("reflection.ConstructorTest");Constructor constructor = classType.getConstructor(new Class[] {int.class, String.class });constructor.newInstance(10, "我路过");//!!!结果是:10 我路过第三个构造方法被调用//访问第四个构造方法,注意啊,权限不同,这是重头戏来着。如下代码Class classType = Class.forName("reflection.ConstructorTest");Constructor constructor = classType.getDeclaredConstructor(new Class[] { String.class });constructor.setAccessible(true);constructor.newInstance(new String("我再路过"));//!!!结果是:我再路过第四个构造方法被调用,这是私有的//首先,不同的地方就是,这次是从所有构造方法中找到这个构造方法//而之前用getConstructor()不包含私有的//然后就是将这个构造器设置为可访问的,setAccessible(true)?

//这是被访问的类public class MethodTest {public void print() {System.out.println("这是print()第一个方法");}public void print(int num) {System.out.println("这是print()第二个方法,有参数" + num);}public String print(String name) {System.out.println("这是print()第三个方法,有参数" + name);return name + "这个是返回的";}private void printPrivate() {System.out.println("这是print()第四个方法,是私有的");}}//访问第一个方法,如下代码Class classtype = Class.forName("reflection.MethodTest");Method method = classtype.getDeclaredMethod("print", new Class[] {});Object object = classtype.newInstance();method.invoke(object, null); //!!!结果是:这是print()第一个方法//访问第二个方法Class classtype = Class.forName("reflection.MethodTest");Method method = classtype.getDeclaredMethod("print",new Class[] { int.class });Object object = classtype.newInstance();method.invoke(object, 10);//!!!结果是:这是print()第二个方法,有参数10//访问第三个方法Class classtype = Class.forName("reflection.MethodTest");Method method = classtype.getDeclaredMethod("print",new Class[] { String.class });Object object = classtype.newInstance();System.out.println(method.invoke(object, "这是传进来的参数哦"));//!!!结果是:这是print()第三个方法,有参数这是传进来的参数哦//!!!结果是:这是传进来的参数哦这个是返回的//访问第四个方法Class classtype = Class.forName("reflection.MethodTest");Method method = classtype.getDeclaredMethod("printPrivate",new Class[] {});method.setAccessible(true);Object object = classtype.newInstance();method.invoke(object, null);//!!!结果是:这是print()第四个方法,是私有的?

?

好吧头痛死了,本来还想写点关于单例模式和反射机制的。以后写单例模式的时候顺带记录吧。

热点排行