Reflection

1.反射
     能够分析类能力的程序称为反射。反射机制可以用来:
     (1)在运行中分析类的能力
     (2)在运行中查看对象
     (3)实现通用的数组操作代码
     (4)利用Method对象,这个对象很想C++中的函数指针
     常用方法:
     (1)static Class forName(String className)
               返回描述类名为className的Class对象
     (2)Object newInstance()
               返回这个类的一个新实例
     (3)Object newInstance(Object[] args)
               构造一个这个构造器所属类的新实例
     (4)void printStackTrace()
               将Throwable对象和栈的轨迹输出到标准错误流
     (5)Field[] getFields()
         Field[] getDeclaredFields()
               getFields方法将返回一个包含对象的数组,这些对象记录了这个类或者其超类的公有域。
               getDeclaredFields方法也将返回包含Field对象的数组,这些对象记录了这个类的全部域。     
     (6)Method[] getMethods()
         Method[] getDeclareMethods()
               返回包含Method对象的数组:getMethods将返回所有的公有方法,包括从超类继承过来的公有方法;
                getDeclareMethods返回这个类或者接口的全部方法,但不包括由超类继承过来的方法。
     (7)Constructor[] getConstructors()
         Constructor[] getDeclaredConstructors()
               返回包含Constructor对象的数组,其中包含了Class对象所描述的所有公有构造器或所有构造器
     (8)Class getDeclaringClass()
               返回一个用于描述类中定义的构造器、方法或域的Class对象
     (9)Class[] getExceptionTypes()
               返回一个用于描述方法抛出的异常类型的Class对象数组
     (10)int getModifiers()
               返回一个用于描述构造器、方法或域的修饰符的整形数值。
     (11)String getName()
               返回一个用于描述构造器、方法或域的字符串
     (12)Class[] getParaeterTypes()
               返回一个用于描述参数类型的Class对象数组
     (13)Class getReturnType()
               返回一个用于描述返回类型的Class对象
     (14)static String toString(int modifiers)
               返回对应modifiers中位设置的修饰符的字符串表示
     (15)static boolean isAbstract(int modifiers)
          static boolean isFinal(int modifiers)
          static boolean isInterface(int modifiers)
          static boolean isNative(int modifiers)
          static boolean isPrivate(int modifiers)
          static boolean isProtected(int modifiers)
          static boolean isPublic(int modifiers)
          static boolean isStatic(int modifiers)
          static boolean isStrict(int modifiers)
          static boolean isSynchronized(int modifiers)
          static boolean isVolatile(int modifiers)
               检测方法名中对应的修饰符在modifiers值中的位

2.获取Class类对象
     Date d = new Date();
     Class c1 = d.getClass();

     String name = c1.getName();
     Class c2  = Class.forName(className);

     Class c1 = Date.class;
     鉴于历史原因,getName应用于数字组类型的时候会返回一个很奇怪的名字

3.Java反射机制中,Field的getModifiers()方法返回int类型值表示该字段的修饰符。其中,该修饰符是java.lang.reflect.Modifier的静态属性。
对应表如下:
PUBLIC: 1
PRIVATE: 2
PROTECTED: 4
STATIC: 8
FINAL: 16
SYNCHRONIZED: 32
VOLATILE: 64
TRANSIENT: 128
NATIVE: 256
INTERFACE: 512
ABSTRACT: 1024
STRICT: 2048

     String modifiers = Modifier.toString(f.getModifiers());
          f.getModifiers()获取到int值
          Modifier.toString(f.getModifiers())把int值转成对应的修饰符
4.API
  (1)void setAccessible(Boolean flag)
     为反射对象设置可访问标志
  (2)boolean isAccessible()
     返回反射对象的可访问标志的值
  (3)static void setAccessible(AccessibleObject[], boolean flag)
     是一种设置对象数组可访问标志的快捷键
  (4)Field getField(String name)
     Field[] getField()
     返回指定名称的公有域,或包含所有域的数组
  (5)Field getDeclaredField(String name)
     Field[] getDeclaredFields()
     返回类中声明的给定名称的域,或者包含声明的全部域的数组 
  (6)java.lang.reflect.Array
     static int getLength(Object array)
     static Object newInstance(Class componentType, int length)
5.Method
     Method类似函数的指针,可以执行任意函数
     Method m = Math.class.getMethod(“sqrt”, double.class);
     m.invoke(null, x)
     由于sqrt是一个静态方法,所以第一个参数可忽略
 

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值