(二)JDBC反射

(二)JDBC反射

public static void main(String[] args) throws Exception {
        //获取类的反射类(模板类)
//        Class userClass = User.class;
        
        //(1)通过类加载获取类的反射类
        Class objClass = Class.forName("com.niit.bean.User");
//        System.out.println(objClass);
        //(2.1)根据类的反射类获取类的实例
        Object obj = objClass.newInstance();
//        System.out.println(obj);
//        if(obj instanceof User){
//            User user = (User)obj;
//            user.getMethod();
//        }
        
        /*------------------------------------------------------*/
        //使用反射类解析该类的内部数据结构
//        //1、获取该类的构造方法对象
//        Constructor constructor = objClass.getConstructor(null);//获取该类的无参构造,该类必须要有无参构造方法才可以用,否则需要传入参数
//        //(2.2)通过构造方法获取该类的实例
//        obj = constructor.newInstance(null);
//        System.out.println(obj);
        
        //获取多个构造方法
//        Constructor[] conArray = objClass.getConstructors();
//        for(Constructor con:conArray){
//            System.out.println(con);
//            //获取构造方法的参数类型
//            Class[] paramType = con.getParameterTypes();
//            for(Class param:paramType){
//                System.out.print(param.getName()+"\t");//getName方法获取类型的名称
//            }
//            
//        }
        //2、使用反射获取该类的属性
//        Field[] fields = objClass.getFields();    //getFields()方法是获得当前可见的属性,可以访问到父类中继承的属性
//        for(Field f:fields){
//            System.out.println(f.getName());
//        }
        
//        Field[] fields = objClass.getDeclaredFields();//getDeclaredFields()方法是获取当前类的所有属性
//        for(Field f:fields){
//            //获取属性名称
//            System.out.println(f.getName());
//            //获取属性类型
//            Class fieldType = f.getType();
//            System.out.println(fieldType.getName());
//            //获取属性的访问修饰符
//            System.out.println(f.getModifiers());//getModifiers()方法返回int类型,根据int值判断哪个访问修饰符
//            
//            if(fieldType == int.class){
//                //获取属性的值
//                System.out.println(f.getInt(obj));//obj表示属性所依赖的对象实例
//            }
//        }
        //获取指定的属性
//        Field field = objClass.getField("userId");
//        System.out.println(field.getInt(obj));
//        
        //3、使用反射获取该类的方法
//        Method[] methods = objClass.getMethods();
//        for(Method m:methods){
//            //获取方法名称
//            System.out.print(m.getName()+"\t");
//            //获取方法返回值类型
//            System.out.print(m.getReturnType()+"\t");
//            Class[] methodClass = m.getParameterTypes();//获取方法参数的类型
//            for(Class mc :methodClass){
//                System.out.print(mc.getName()+"\t");
//            }
//            System.out.println();
//        }
        
        //获取指定的方法对象
        Method method = objClass.getMethod("getMethod"null);
        //使用反射调用方法,第一个参数表示方法依赖的类的实例对象,第二个参数表示方法需要的参数值
        method.invoke(obj, null);
        
        Method method2 = objClass.getMethod("getValue", String.class,int.class);
        //获取返回值
        Object returnValue = method2.invoke(obj, "testValue",200);
        System.out.println(returnValue);
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值