• java>反射初体验


        public static void main(String args[])
        {
            Class c = null;
            Package p = null;
            Constructor constructors[];
            Field fields[];
            Method methods[];
            
            int i,j;
            
            try{
                c = Class.forName("java.net.URLDecoder");
                
                //输出的package的名称
                System.out.println("package:");
                p = c.getPackage();
                if(p != null)
                {
                    System.out.println(p.getName());
                }
                
                // 输出class或interface的名称,及其修饰符(modifiers)
                System.out.println();
                System.out.println("class:");
                int mod = c.getModifiers();
                System.out.print(Modifier.toString(mod)); //整个modifier
                if(Modifier.isInterface(mod))
                {
                    System.out.print(" ");       //modifier内已经包含关键词 "interface"
                }
                else
                {
                    System.out.print(" class "); //关键词 "class"
                }
                System.out.println(c.getName());
                
                
                //输出构造函数
                System.out.println();
                System.out.println("constructors:");
                constructors = c.getConstructors();
                for(i=0; i<constructors.length; i++)
                {
                    
                    //System.out.print(constructors[i].toGenericString());
                    //上述语句输出:public java.util.LinkedList(java.util.Collection<? extends E>)
                    
                    //下面的语句合作输出:public java.util.LinkedList(java.util.Collection arg0)
                    //这两种方法的输出结果大同小异
                    System.out.print(Modifier.toString(constructors[i].getModifiers()));
                    System.out.print(" " + constructors[i].getName());
                    System.out.print("(");
                    Class cx[] = constructors[i].getParameterTypes();
                    if(cx.length >= 1)
                    {
                        System.out.print(cx[0].getName());
                        System.out.print(" arg0");
                    }
                    for(j=1; j<cx.length; j++)
                    {
                        System.out.print(", " + cx[j].getName());
                        System.out.print(" arg" + j);
                    }
                    System.out.println(")");
                    
                }
                
                //输出变量
                System.out.println();
                System.out.println("variables:");
                fields = c.getDeclaredFields();
                for(i=0; i<fields.length; i++)
                {
                    System.out.print(Modifier.toString(fields[i].getModifiers()));
                    System.out.print(" " + fields[i].getType().getName());
                    System.out.println(" " + fields[i].getName());
                }
                
    
                //输出方法
                System.out.println();
                System.out.println("methods:");
                methods = c.getDeclaredMethods();
                for(i=0; i<methods.length; i++)
                {
                    System.out.print(Modifier.toString(methods[i].getModifiers()));
                    Class returnClass = methods[i].getReturnType();
                    System.out.print(" " + returnClass.getName() + " " + methods[i].getName() + "(");
                    
                    Class cx[] = methods[i].getParameterTypes();
                    if(cx.length >= 1)
                    {
                        System.out.print(cx[0].getName());
                        System.out.print(" arg0");
                    }
                    for(j=1; j<cx.length; j++)
                    {
                        System.out.print(", " + cx[j].getName());
                        System.out.print(" arg" + j);
                    }
                    
                    System.out.println(")");            
                }
                
                
                //输出父类
                System.out.println();
                System.out.println("super class:");
                Class superClass = c.getSuperclass();
                if(superClass != null)
                {
                    System.out.println(superClass.getName());    
                }
                
                //输出实现的接口
                System.out.println();
                System.out.println("implemented interfaces:");
                Class interfaces[] = c.getInterfaces();
                for(i=0; i<interfaces.length; i++)
                {
                    System.out.print(interfaces[i].getName() + ", ");
                }
                System.out.println();
                
                //输出inner classes
                System.out.println();
                System.out.println("inner classes:");
                Class innerClasses[] = c.getDeclaredClasses();
                for(Class innerClass : innerClasses)
                {
                    System.out.print(innerClass.getName() + ", ");
                }
                System.out.println();
                
                //输出outer classes
                System.out.println();
                System.out.println("outer classes:");
                Class outerClass = c.getDeclaringClass();
                if(outerClass != null)
                    System.out.println(outerClass.getName());
                else
                    System.out.println("null");
    
                
                
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        }

    运行结果如下图:

  • 相关阅读:
    优步中国大举扩张,2016进军100城市!
    优步中国:2月底前进军广东、湖南、湖北18个城市
    Uber能知道你是不是在开车的时候玩手机
    Uber入驻四川乐山峨眉地区
    南京优步上线黄金区域,接单可享更高奖励!
    优步UBER司机全国各地奖励政策汇总 (2月1日-2月7日)
    成都Uber优步司机奖励政策(2月1日)
    北京Uber优步司机奖励政策(2月1日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(2月1日)
    跨程序共享数据,读取联系人信息
  • 原文地址:https://www.cnblogs.com/zzj2/p/2983489.html
Copyright © 2020-2023  润新知