Java 反射机制[Field反射]

Java 反射机制[Field反射]

1.  反射概念及功能

          反射就是把Java类中的各种成分映射成相应的Java类。例如一个Java类中用一个Class类的对象来表示。一个类中的组成部分分为成员变量,方法,构造方法,包等等。

          Java反射机制主要提供了以下功能: 判断在运行时任意一个对象所属的类;在运行时构造任意一个类的对象;判断在运行时任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。

2.  Field反射

         以下代码将obj对象中的String类型的字段对应的Value中含有i的字符替换为abc。通过这个简单的案例进行初步分析Java的反射机制。

Java Code

/*

* System Abbrev :

* system Name  :

* Component No  :

* Component Name:

* File name     :ReflectTestMain.java

* Author        :Qiuzhenping

* Date          :2014-10-25

* Description   :  <description>

*/

 

/* Updation record 1:

 * Updation date        :  2014-10-25

 * Updator          :  Qiuzhenping

 * Trace No:  <Trace No>

 * Updation No:  <Updation No>

 * Updation Content:  <List all contents of updation and all methods updated.>

 */

package com.qiuzhping.reflect.main;

 

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

 

/**

 * <Description functions in a word>

 * 反射就是把Java类中的各种成分映射成相应的Java类。

 * <Detail description>

 *

 * @author  Qiuzhenping

 * @version  [Version NO, 2014-10-25]

 * @see  [Related classes/methods]

 * @since  [product/module version]

 */

public class ReflectTestMain {

 

         /** <default constructor>

          */

         public ReflectTestMain() {

                   // TODO Auto-generated constructor stub

         }

 

         /** <Description functions in a word>

          * 2014-10-25

          * <Detail description>

          * @author  Qiuzhenping

          * @param args [Parameters description]

          * @return void [Return type description]

          * @exception throws [Exception] [Exception description]

          * @see [Related classes#Related methods#Related properties]

          */

         public static void main(String[] args) throws Exception{

                   // TODO Auto-generated method stub

                   //Constructor[] contructor = Person.class.getConstructors();

                   //Method[] method = Person.class.getMethods();

                   Person p = new Person(24, "Qiuzhping", "100001", "Qiuzhping");

//               Field [] field = p.getClass().getDeclaredFields();

//               for(Field f:field){

//                         f.setAccessible(true);

//                         System.out.println(f.getName());

//                         Object obj = f.get(p);

//                         System.out.println(obj);

//               }

                   changeStringValue(p);

                   System.out.println(p.toString());

         }

 

         /** <Description functions in a word>

          *  将obj对象中的String类型的字段对应的Value中含有i的字符替换为abc<BR>

          *  2014-10-26

          * <Detail description>

          * @author  Qiuzhenping

          * @param obj [Parameters description]

          * @return void [Return type description]

          * @exception throws [Exception] [Exception description]

          * @see [Related classes#Related methods#Related properties]

          */

         private static void changeStringValue(Object obj) throws Exception {

                   Field[] fields = obj.getClass().getDeclaredFields();

                   for(Field f : fields){

                            f.setAccessible(true);//暴力反射

                            if(f.getType() == String.class){//字节码比较是用 ==

                                     String oldValue = (String) f.get(obj);

                                     String newValue = oldValue.replaceAll("i", "abc");//将所有的i替换为abc

                                     f.set(obj, newValue);

                            }

                   }

                  

         }

        

          static class Person {

 

                   public Person(int age, String name, String id, String pwd) {

                            super();

                            this.age = age;

                            this.name = name;

                            this.id = id;

                            this.pwd = pwd;

                   }

                   @Override

                   public String toString() {

                            return "age = "+age +"\tname = "+name+"\tid = "+id+"\tpwd = "+pwd;

                   }

                   private int age;

                   private String name;

                   private String id;

                   private String pwd;

                   public int getAge() {

                            return age;

                   }

                   public void setAge(int age) {

                            this.age = age;

                   }

                   public String getName() {

                            return name;

                   }

                   public void setName(String name) {

                            this.name = name;

                   }

                   public String getId() {

                            return id;

                   }

                   public void setId(String id) {

                            this.id = id;

                   }

                   public String getPwd() {

                            return pwd;

                   }

                   public void setPwd(String pwd) {

                            this.pwd = pwd;

                   }

                   /** <default constructor>

                    */

                   public Person() {

                            // TODO Auto-generated constructor stub

                   }

 

         }

}

 

Java 反射机制[Method反射] 


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值