反射

反射机制:reflection
主线:Class-Field-Constructor-Method-Annotation
1.可通过类名加载类;
2.获得类的 类名,属性,方法,父类;
3.调用静态方法,加setAccessiable(true);

获得类实例:
1.Class c=obj.getClass();    通过对象获得类实例;
2.Class c1=Class.forName("com.oracle...");
3.Class c2=Integer.Class;
常用方法:Class<?> c=obj.getClass();
1.获得类名:String  c.getName();全称    c.getSimpleName();简称
2.获得父类:Class c.getSuperclass();
3.获得java修饰符:int c.getModifiers(); 
4.将修饰符int转化为String:Modifier.toString();
5.获得参数的个数:int getParameterCount();
6.获得参数类型;可通过 Class[] getParameterTypes();    
7.判断是否为接口类:boolean c.isInterface();    获取实现的接口:Class[] c.getInterfaces();
8.是否是一个数组类:boolean c.isArray();        数组组件类型:Class c.getComponentType();

9.Field属性的元数据封装;
Field [] getFields();        获得公共属性;
Field getField(String name):    根据属性名获得公共属性;
Field getDeclaredFields(String name);    获得自定义的属性;
Field [] getDeclaredField();        根据属性名获得自定义属性;
利用反射赋值:s为对象:
    Classc=s.getClass();
    Field f=c.getDeclaredField("");
    (f.setAccessiable(true))f.set(s," ");  //属性.set(对象,值);
    f.get(s);    //属性.get(对象);
注:获得父类私有:.getSuperclass().getDeclaredFields();
10.构造器:Constructor-获得构造方法;
Constructor<T> getConstructor(Class...args);    根据参数列表获得公共的构造方法;
Constructor<T> []      getConstructors();        获得所有公共的构造方法;
Constructor<T> getDeclaredConstructor(Class...args);根据列表参数获得构造方法;    
Constructor<T>[] getDeclaredConstructors();    获得所有当前类的构造方法;
实例一个无参的对象:
1.Object  .newInstance(参数);    创建一个对象;无参的-通过获取的构造方法创建一个对象;

应用场景:
1.调用private构造方法;
2.调用默认构造方法,Class.newInstance();

 

//1.执行sql    
Class<T> clas,String sql,Object...args;   
     ps.setObject(i+1,args[i]);    
ResultSet rs=ps.executeQuery();
//2.转换成vo;   
 Field[] field=clas.getDeclaredFields();   
     while(rs.next()){   
     T obj=clazz.newInstance();
    //创建实例T;   
     for(Field f:field){    
        f.setAccessible(true);    
        if(f.getType==Integer.class){   
             f.set(obj, rs.getInt(f.getName()));   
     }else if(f.getType==String.class){    
        f.set(obj, rs.getString(f.getName()));    
        }else{   
         f.set(obj,rs.getObject(f.getName()));    
        }   
     }
        list.add(obj);   
 }


11.Method
类中定义的方法;
修饰符-返回值类型-方法名-参数
Method [] getMethods();        获得公共方法;
Method getMethod(String name,Class...obj):    根据属性名获得公共方法;
Method getDeclaredMethods(String,Class...obj);    获得指定的方法,不包括继承;
Method [] getDeclaredMethod();    根据属性名获得自定义方法;

1.返回类型 Class:getReturnType();
2.java修饰符 int:getModifiers();
3.获得参数类型:getParameterTypes();
4.获取方法名:getName();
5.对带有指定参数的指定对象调用由此 Method 对象表示的底层方法:
    Object invoke(Object target, Object...params)  
使用原则:
//1.获得方法,设置Accessible;
Method f=b.getClass().getDeclaredMethod("",Integer.class);
m.setAccessible(true);
//2.调用方法;方法对象参数;
m.invoke(b,100);

12.Annotation:注解:
    给编译器看的给元数据添加的补充说明:
    元数据:Class Field Constructor Methods等
一.解决的问题:
    1.解决数据库和java类中类名不匹配的问题;
    2.解决无法在类中设置主键的问题;
二.分类
1.jdk提供的
    @Override    重写,仅限方法
    @Deprecated    设置为过期
    @SuppressWarings    压制警告
            @SuppressWarings("deprecation")-加在代码前
            @SuppressWarings("unused")
            @SuppressWarings({"deprecation","unused"})-加在方法前
2.自定义注解
    1.@PrimaryKey
    Add @Retention  
        Source源代码中(Override) Class编译class结束 Runtime运行阶段
    Add @Target Type 
        Field Method Parameter Constructor ...
    程序中:
    @Retention(RUNTIME)    
    @Target(FIELD)    //多个时候用({ , })
    public @interface PrimaryKey{
    //标识主键可以直接在类中标识@PrimaryKey;
    }
    
    

 

 

 

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值