Java反射的基本内容总结

本文深入解析Java反射机制,介绍如何通过.class和getClass()方法获取Class对象,演示如何使用Class对象获取类的修饰符、字段、方法及构造器,并展示如何访问和修改私有字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Class<?> clazz=  Child.getClass();
Java的每个类都带有一个运行时类对象,该Class对象中保存了创建对象所需的所有信息。

1、通过某种类型的.class获取取Class对象

   Class<?> c=int.class;

2、通过引用对象的getClass() 方法获取Class对象

Object o=new StringBuffer();
Class<?> c=o.getClass();

注:可直接用某种类型的.class返回此 Object 的运行时类Class对象,也可以用某种类型的引用对象的 .getClass()方法获得。获得此对象后可以利用此Class对象的一些反射特性进行操作

   int mod = clazz.getModifiers(); // 以整数形式返回 类的 修饰符
    System.out.println( mod );
    String modifiers = Modifier.toString( mod ) ; 
 // Modifier 类中的 类方法 toString 可以将 整数形式的 修饰符 转换为 字符串形式

 System.out.println( "~~~~~~~~~~~~ 名称 ~~~~~~~~~~~~~~" );
    System.out.println( "类名: " + clazz.getName() );
    System.out.println( "简单类名: " + clazz.getSimpleName() );
    System.out.println( "规范化类名: " + clazz.getCanonicalName()  );

System.out.println( “~~~~~~~~~ 获取 " + clazz.getSimpleName() + " 直接声明的所有字段 ~~~~~~~~~” );

    // 获取 指定类 中 直接声明的 所有的 字段 ( Field ) ( 不考虑修饰符 )
    Field[] declaredFields = clazz.getDeclaredFields();

System.out.println( “~~~~~~~~~ 从” + clazz.getSimpleName() + " 类中获取 名称是 third 的字段 ~~~~~~~~~"

Field sizeField = clazz.getDeclaredField( "third" ); // 如果        未找到则抛  出 NoSuchFieldException 异常
    System.out.println( sizeField );

System.out.println( “~~~~~~~~~ 获取 " + clazz.getSimpleName() + " 所有 public 字段(含从父类继承的 public 字段) ~~~~~~~~~” );

    Field[] fields = clazz.getFields();

System.out.println( “~~~~~~~~~ 从 " + clazz.getSimpleName() + " 所有 public 字段(含从父类继承的 public 字段) ~" );
System.out.println( "
~ 获取 指定名称的字段 ~~~~~~~~~” );

    Field f = clazz.getField( "publicField" );

System.out.println( “~~~~~~~~~~~~ 获取 " + c.getSimpleName() +” 类中所有的 public 修饰的方法(包括从父类继承的 public 方法 ) ~~~~~~~~~~~~~~~~~~" );

    Method[] methods = c.getMethods();

System.out.println( “~~~~~~~~~ 从 " + c.getSimpleName() +” 类中可以用的 public 修饰的方法(包括从父类继承的 public 方法 ) 寻找指定方法~~~~~~~~~~" );

    // 获取具体某个方法对应的 Method 对象时,需要指定 方法名称 和 参数类型列表
    Method method = c.getMethod( "wait"  , long.class , int.class );
    System.out.println( method );
    method = c.getMethod( "enen"  );

System.out.println( “~~~~~~~~~~~~ 获取 " + c.getSimpleName() +” 类中直接声明的所有的方法(所有权限修饰符修饰的方法) ~~~~~~~~~~~~~~~~~~" );

    Method[] declaredMethods = c.getDeclaredMethods();

System.out.println( “~~~~~~~~ 从 " + c.getSimpleName() +” 类直接声明的所有的方法(所有权限修饰符修饰的方法)中获取指定方法 ~~~~~~~~~~~~~" );

    // 获取具体某个方法对应的 Method 对象时,需要指定 方法名称 和 参数类型列表
    Method declaredMethod = c.getDeclaredMethod( "haha" );
字段访问权限的设置
final Object o = new Child();
        final Object x = new Child();

        Class<?> c = o.getClass(); // 获得 o 变量 所引用的 对象的 类型

        Field f = c.getDeclaredField( "first" ); // 获取 c 对应的类中直接声明的 名称是 first 的字段

        // 如果 当前类不具备访问 o 引用的对象中 f 对应的字段的权限,则使用 get 和 set 时会抛出 IllegalAccessException
        f.setAccessible( true ); // 让不能被访问的 字段 可以被访问

        // 获取 o 所引用的 Child 对象中 third 字段的值  o.third
        Object value = f.get( o ) ; // 从 o 引用的对象中 获取 f 所表示的 字段的值 :  Object value = o.first ;

        System.out.println( value );

        f.set( o , "第一" ); // 为 o 引用的对象中 f 所表示的字段 赋值 : o.first = "第一" ;

        value = f.get( o ) ;
        System.out.println( value );

        System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~" );

        System.out.println( f.get( x ) ); // 输出 x 所引用的对象中 f 所表示的 字段的值 : x.first
        f.set( x , "第一X"); // 为 x 所引用的对象中 f 所表示的字段赋值 :  x.first = "第一X" ;
        System.out.println( f.get( x ) );
构造器访问权限的设置
   Class<?> c = Eagle.class ;


        Constructor<?>  constructor = null ;

        // 获取无参构造
        constructor = c.getDeclaredConstructor(); // 获取构造方法

        System.out.println( System.identityHashCode( constructor ) );

        // 与 Field 、Method 相同,当当前程序不具备对某个构造方法的访问权限时,可以使用 setAccessible 来设置
        constructor.setAccessible( true );
        // 使用 Constructor 类中的 newInstance( Object... args ) 方法来构造对象
        Object o = constructor.newInstance(); // Object o = new Eagle();
        System.out.println( o );

        System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );

        constructor = c.getConstructor( Integer.class , String.class );

        System.out.println( System.identityHashCode( constructor ) );

        constructor.setAccessible( true );
        // 使用 Constructor 类中的 newInstance( Object... args ) 方法来构造对象
        Object x = constructor.newInstance( 100 , "苍鹰" );
        System.out.println( x );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值