首页 > 其他 > 详细

编写程序,熟悉反射机制

时间:2020-03-16 16:12:26      阅读:51      评论:0      收藏:0      [点我收藏+]
import java.lang.reflect.*;

public class TestReflection{

    public static void main(String[] args) throws Exception{
        String str = "T";
        Class c = Class.forName(str);
        T t =(T)c.newInstance();
        for(Method m : c.getMethods()){
            if(m.getName().equals("m1")){
                m.invoke(t,234);
                System.out.println(m.getReturnType().getName());
                for(Class temp : m.getParameterTypes()){
                    System.out.println(temp.getName());
                }
            }
            
            if(m.getName().equals("m2")){
                m.invoke(t,2,5.0);
                System.out.println(m.getReturnType().getName());
                m.getParameterTypes();
                for(Class temp : m.getParameterTypes()){
                    System.out.println(temp.getName());
                }
            }
        }
    }

}

class T{
    
    static{
        System.out.println("static");
    }
    
    int i;
    double j;
    
    public int m1(int i){
        this.i = i;
        System.out.println("i = " + i);
        return i;
    }
    
    public double m2(int i , double j){
        this.i = i;
        this.j = j;
        System.out.println("i + j = " + (i + j));
        return i + j;
    }
    
}

 

编写程序,熟悉反射机制

原文:https://www.cnblogs.com/yxfyg/p/12504441.html

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