JAVA 注解和反射

通过反射来获取类

Class MyTest{
    private String name;
    public String showName{
        System.out.println(this.name);
    }
}
Class myClass = Class.forName("MyTest");    //代替JVM引入MyTest类
Method myMethod = myClass.getDeclaredMethod("showName", String.class);  //获取方法
Object myObj = myClass.newInstance(); //实例化类
myMethod.invoke(myObj,"smartom");     //调用方法

猜你喜欢

转载自www.cnblogs.com/subtract/p/8989385.html