首页 > 编程语言 > 详细

java反射小练习

时间:2014-05-16 07:11:36      阅读:367      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package com.gmw;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectTest {
    void test(String name,String word){
        System.out.println(name+"说"+word);
    }
    public ReflectTest(String name) {    }
    
    public ReflectTest() {}
    
    public static void main(String[] args) {
            try {
                Class clazz = Class.forName("com.gmw.ReflectTest");
                Constructor<?> cons = clazz.getConstructor(String.class);
                ReflectTest test = (ReflectTest)cons.newInstance("");
                test.test("babybear", "我们分手吧");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException 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 (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}
bubuko.com,布布扣

 

java反射小练习,布布扣,bubuko.com

java反射小练习

原文:http://www.cnblogs.com/baby-bear/p/3726101.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!