码迷,mamicode.com
首页 > 编程语言 > 详细

java反射小练习

时间:2014-05-16 07:13:08      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

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反射小练习

标签:style   blog   class   code   c   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!