java SE - reflect

java SE - reflect

测试类

复制代码
package testReflect;

/** 
 * @Package testReflect

 * @ClassName: Hello

 * @Description: TODO()

 * @author andy

 * @date 2013-5-31 上午09:59:29

 */
public class Hello {
    public String name;
    private int id = 1;
    
    public Hello(){
        
    }
    public Hello(String name,int id){
        this.name = name;
        this.id = id;
    }
    
    
    public String getUserName(){
        return "123";
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }
}
复制代码

获得某个类或对象的运行时Class的对象的三种方式

复制代码
package testReflect;

/** 
 * @Package testReflect

 * @ClassName: InstancingClass

 * @Description: TODO(获得某个类或对象的运行时Class的对象的三种方式)

 * @author andy

 * @date 2013-5-31 上午09:59:05

 */
public class InstancingClass<T> {
    public static void main(String[] args) throws ClassNotFoundException {
        //第一种
        Class<Hello> classType1 = Hello.class;
        //第二种
        Class<?> classType2 = Class.forName("testReflect.Hello");
        //第三种
        Hello hello = new Hello();
        Class<? extends Hello> classType3 = hello.getClass();
        
        System.out.println(classType1 + "\n" +classType2 + "\n" + classType3 + "\n");
        System.out.println(classType1.getName() + "\n" +classType2.getName() + "\n" + classType3.getName() + "\n");
        System.out.println(classType1.getSimpleName() + "\n" +classType2.getSimpleName() + "\n" + classType3.getSimpleName() + "\n");
    }
}
复制代码

不用new关键字实例化对象的三种方法

复制代码
package testReflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/** 
 * @Package testReflect

 * @ClassName: InstancingObject

 * @Description: TODO(不用new关键字实例化对象的三种方法)

 * @author andy

 * @date 2013-5-31 上午10:08:34

 */
public class InstancingObject {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        Hello hello0 = Hello.class.newInstance();
        
        Class<Hello> classType = Hello.class;
        
        //构造函数没有参数时,第一种等价于第二种
        //第一种
        Hello hello1 = (Hello) classType.newInstance();
        
        //第二种
        Constructor<Hello> con = classType.getConstructor();
        Hello hello2 = (Hello) con.newInstance();
        
        //第三种
        Constructor<Hello> con2 = classType.getConstructor(new Class[]{String.class, int.class});
        Hello hello3 =  (Hello)con2.newInstance(new Object[]{"123",123});
        
        System.out.println(hello0.getUserName());
        System.out.println(hello1.getUserName());
        System.out.println(hello2.getUserName());
        System.out.println(hello3.getUserName());
    }
}
复制代码

通过反射对字段的操作

复制代码
package testReflect;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;

/** 
 * @Package testReflect

 * @ClassName: AccessFieldByReflect

 * @Description: TODO(通过反射对字段的操作)

 * @author andy

 * @date 2013-5-31 上午10:48:13

 */
public class AccessFieldByReflect {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        Hello hello = Hello.class.newInstance();
        Field[] fields = hello.getClass().getDeclaredFields();
        //遍历获取所有的字段
        for(Field field : fields){
            //System.out.println(field);
            System.out.println(field.getName());
        }
        System.out.println("没有修改前:" + hello.getName());
        System.out.println("没有修改前:" + hello.getId());
        for(Field field : fields){
            if("id".equals(field.getName())){
                //允许访问所有Field、Method 和 Constructor 私有元素
                field.setAccessible(true);
                //指定只允许访问的Field
                AccessibleObject[] accessParam = {field};
                Field.setAccessible(accessParam,true);
                //如果那个字段是int就setint,是double就setdouble
                field.setInt(hello, 123);
            }else{
                //字段是字符串
                field.set(hello, "张三");
            }
        }
        
        System.out.println("通过反射修改后:" + hello.getId());
        System.out.println("通过反射修改后:" + hello.getName());
    }
}
复制代码

通过反射对方法的操作

复制代码
package testReflect;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/** 
 * @Package testReflect

 * @ClassName: AccessMethodByReflect

 * @Description: TODO(通过反射对方法的操作)

 * @author andy

 * @date 2013-5-31 上午11:25:39

 */
public class AccessMethodByReflect {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException {
        Hello hello = Hello.class.newInstance();
        Method[] methods = hello.getClass().getDeclaredMethods();
        //遍历获取所有的字段
        for(Method method : methods){
            //System.out.println(field);
            System.out.println(method.getName());
        }
        System.out.println("没有修改前:" + hello.getName());
        System.out.println("没有修改前:" + hello.getId());
        for(Method method : methods){
            if("setId".equals(method.getName())){
                //允许访问所有Field、Method 和 Constructor 私有元素
                method.setAccessible(true);
                //只允许访问指定的Method
                AccessibleObject[] accessParam = {method};
                Method.setAccessible(accessParam,true);
            }else if("setName".equals(method.getName())){
                method.invoke(hello, "张三");
            }
        }
        
        String obj = (String) hello.getClass().getDeclaredMethod("getName").invoke(hello); 
        System.out.println("通过反射getName方法返回来的值:" + obj);
        System.out.println("通过反射修改后:" + hello.getId());
        System.out.println("通过反射修改后:" + hello.getName());
        
    }
}
复制代码

 

 

 

Integer.TYPE 返回的int,即返回的是它们所对应的原生数据class对象(所有的包装类都是这样的),

Integer.class 返回的是它所对应的Class对象 java.lang.Integer

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值