我是靠谱客的博主 沉静火龙果,最近开发中收集的这篇文章主要介绍JAVA反射系列之Field,java.lang.reflect.Field使用获取方法。,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述



</pre>转载自<a target=_blank target="_blank" href="https://my.oschina.net/u/1407116/blog/209383"> https://my.oschina.net/u/1407116/blog/209383</a><p></p><p style="">首先必须明一点 <span style=""><span style="">Field类主要是用来辅助获取和操作类的属性的</span></span>!</p><p style=""><span style="">1.怎么通过反射获取类的属性</span></p><p style="">     先来看JDK提供的方法有如下几种:</p><p style="">         <code style=""><span style="">a)Class.getDeclaredField</span>(String name);</code></p><p style=""><code style="">     返回<span style="">一个</span> <code style="">Field</code> 对象,该对象反映此 <code style="">Class</code> 对象所表示的类或接口的指定<span style="">已声明字段</span>。</code></p><p style=""><code style="">     <code style=""><span style="">b)Class.getDeclaredFields</span>();</code></code></p><p style=""><code style=""><code style="">      返回 <code style="">Field</code> 对象的<span style="">一个数组</span>,这些对象反映此 <code style="">Class</code> 对象所表示的类或接口所<span style="">声明的所有字段</span>。</code></code></p><p style=""><code style=""><span style="">    c)</span><code style=""><span style="">Class.getField</span>(String name)</code>;</code></p><p style=""><code style="">       返回<span style="">一个 </span><code style="">Field</code> 对象,它反映此 <code style="">Class</code> 对象所表示的类或接口的指定<span style="">公共成员字段</span>。</code></p><p style=""><code style=""><span style="">    d)</span><code style=""><code style=""><span style="">Class.getField</span>()</code></code>;</code></p><p style="">              返回一个包含某些 <code style="">Field</code> <span style="">对象的数组</span>,这些对象反映此 <code style="">Class</code> 对象所表示的类或接口的<span style="">所有可访问公共字段</span>。</p><p style="">    来一个例子来说明一下 :</p><p style="">   实体类:<br style="" /></p><p style=""></p><pre name="code" class="java"> import java.util.Date;
/**
 * @ClassName: Student
 * @Desc
</pre><br />ription<span class="hljs-comment" style="">: 学生实体 * </span><span class="hljs-doctag" style=""><span class="hljs-comment" style=""><span class="hljs-doctag" style="">@author</span></span></span><span class="hljs-comment" style=""> JoseanLuo   * </span><span class="hljs-doctag" style=""><span class="hljs-comment" style=""><span class="hljs-doctag" style="">@date</span></span></span><span class="hljs-comment" style=""> 2014-3-18 下午5:17:35 * </span><span class="hljs-doctag" style=""><span class="hljs-comment" style=""><span class="hljs-doctag" style="">@version</span></span></span><span class="hljs-comment" style=""> V1.0  */</span><span class="hljs-keyword" style="">public</span> <span class="hljs-class" style=""><span class="hljs-keyword" style=""><span class="hljs-class" style=""><span class="hljs-keyword" style="">class</span></span></span><span class="hljs-class" style=""> </span><span class="hljs-title" style=""><span class="hljs-class" style=""><span class="hljs-title" style="">Student</span></span></span><span class="hljs-class" style=""></span></span>{  <span class="hljs-keyword" style="">private</span> Long id;  <span class="hljs-keyword" style="">private</span> String name;  <span class="hljs-keyword" style="">private</span> Date createdate;  <span class="hljs-keyword" style="">private</span> String no;  <span class="hljs-keyword" style="">public</span> String nickname;<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> Long </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">getId</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">()</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">return</span> id;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">void</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">setId</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">(Long id)</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">this</span>.id = id;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> String </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">getName</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">()</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">return</span> name;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">void</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">setName</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">(String name)</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">this</span>.name = name;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> Date </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">getCreatedate</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">()</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">return</span> createdate;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">void</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">setCreatedate</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">(Date createdate)</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">this</span>.createdate = createdate;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> String </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">getNo</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">()</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">return</span> no;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">void</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">setNo</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">(String no)</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">this</span>.no = no;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> String </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">getNickname</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">()</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">return</span> nickname;}<span class="hljs-function" style=""><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">public</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-keyword" style=""><span class="hljs-function" style=""><span class="hljs-keyword" style="">void</span></span></span><span class="hljs-function" style=""> </span><span class="hljs-title" style=""><span class="hljs-function" style=""><span class="hljs-title" style="">setNickname</span></span></span><span class="hljs-params" style=""><span class="hljs-function" style=""><span class="hljs-params" style="">(String nickname)</span></span></span><span class="hljs-function" style=""></span></span>{    <span class="hljs-keyword" style="">this</span>.nickname = nickname;}  }
     测试类:
import java.lang.reflect.Field;
import java.util.Date;
/**
 * @ClassName: ReflectFieldTest
 * @Description: 反射Field测试。
 * @author JoseanLuo  
 * @date 2014-3-18 下午5:16:17
 * @version V1.0 
 */
public class ReflectFieldTest
{
 
    public static void main(String[] args)
{    Student stu=new Student();
     stu.setId(1L);
     stu.setName("Josean");
     stu.setNo("201403185203344");
     stu.setCreatedate(new Date());
     try
    {
        Field property1=stu.getClass().getDeclaredField("name");
        System.out.println(property1);//private java.lang.String com.cx.test.Student.name
        Field property3=stu.getClass().getField("nickname");
        System.out.println(property3);//public java.lang.String com.cx.test.Student.nickname
        //错误方法 getField系列方法只能获取公共字段
        //Field property2=stu.getClass().getField("name");
        //System.out.println(property2);
        //会抛java.lang.NoSuchFieldException
        
        
    } catch (SecurityException e)
    {
        
        e.printStackTrace();
    } catch (NoSuchFieldException e)
    {
        
        e.printStackTrace();
    }
}
    
    
}

  

2.进行属性获取更改

   得到这个Field之后你就可以获取他的值或者设置他的值了。

   获取他的值用get类型方法,他针对常见类型提供了对应get方法,这里就不一一列举了。

   值得注意的是获取私有属性的时候必须先设置Accessible为true,然后才能获取。

   同理设置的时候调用set类型方法,这里也不一一列举了,下面放代码。

import java.lang.reflect.Field;
import java.util.Date;
/**
 * @ClassName: ReflectFieldTest
 * @Description: 反射Field测试。
 * @author JoseanLuo  
 * @date 2014-3-18 下午5:16:17
 * @version V1.0 
 */
public class ReflectFieldTest
{
 
    public static void main(String[] args) throws Exception
{    Student stu=new Student();
     stu.setId(1L);
     stu.setName("Josean");
     stu.setNo("201403185203344");
     stu.setCreatedate(new Date());
     stu.setNickname("copyman");
        Field property1=stu.getClass().getDeclaredField("name");
        //System.out.println(property1);//out:private java.lang.String com.cx.test.Student.name
        Field property3=stu.getClass().getField("nickname");
        System.out.println(property3.get(stu));
        //System.out.println(property3);//out:public java.lang.String com.cx.test.Student.nickname
        //错误方法 getField系列方法只能获取公共字段
        //Field property2=stu.getClass().getField("name");
        //System.out.println(property2);
        //会抛java.lang.NoSuchFieldException
        Field [] prFields4=stu.getClass().getDeclaredFields();
        for(Field field:prFields4)
        {
            System.out.println(field);
            System.out.println(field.equals(property1));
            //私有变量必须先设置Accessible为true
            field.setAccessible(true);
            //获取用get类方法。
            System.out.println(field.get(stu));
        }
        //设置用set类方法
        property3.set(stu, "名字被我改了,哈哈");
         
        System.out.println(stu.getNickname());
        
}
    
    
}

这个是控制台输出:

copyman
private java.lang.Long com.cx.test.Student.id
false
1
private java.lang.String com.cx.test.Student.name
true
Josean
private java.util.Date com.cx.test.Student.createdate
false
Tue Mar 18 18:19:39 CST 2014
private java.lang.String com.cx.test.Student.no
false
201403185203344
public java.lang.String com.cx.test.Student.nickname
false
copyman
名字被我改了,哈哈


最后

以上就是沉静火龙果为你收集整理的JAVA反射系列之Field,java.lang.reflect.Field使用获取方法。的全部内容,希望文章能够帮你解决JAVA反射系列之Field,java.lang.reflect.Field使用获取方法。所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部