反射知识

反射知识

1、创建对象:

(1)使用无参构造方法:

Class c = Class.forName("java.util.ArrayList");

LIst list = (List)c.newInstance();

(2)使用反射机制调用无参构造方法创建指定名称类的对象:


import java.util.Date;

public class NoArgCITest {


public static void main(String[] args) {
try {
Date currentDate = (Date)newInstance("java.util.Date");//获取Class对象
System.out.println(currentDate);

} catch (Exception e) {


e.printStackTrace();
}


}


private static Date newInstance(String string) {
Object obj = null;
try {
obj = Class.forName(string).newInstance();
} catch (Exception e) {


e.printStackTrace();


return null;
}


}


(3)使用带参数的构造方法:3个步骤:

<1>获取指定类对应的Class对象

<2>通过Class对象获取满足指定参数类型要求的Constructor对象

<3>调用指定Constructor对象的newInstance方法,传入对应的参数值,创建对象。

(4)通过java.util.Data类的带参构造方法来创建对象。


import java.lang.reflect.Constructor;

import java.util.Date;


public class ArgCITest {


public static void main(String[] args) {
Class clazz;
try {
clazz = Class.forName("java.util.Date");
Constructor constructor = clazz.getConstructor(long.class);
Date d = (Date) constructor.newInstance(1234L);
System.out.println(d);

} catch (Exception e) {


e.printStackTrace();
}



}


}


2、<1>调用方法:使用反射可以获取指定类的指定方法的对象代表即java.lang.reflect.Method类的实

例。invoke方法动态调用该方法。

public Object invoke(Object obj,Object ...args);

    <2>第一个参数是一个对象,表示要在该对象上调用这个方法;第二个参数是一个可变参数,用来给这个方法传递参数值。

    <3>要调用某个私有方法可以在这个私有对应的Method对象上先调用setAccessible(true)来取消Java语言对本方法的访问检查,然后再调用invoke方法来真正执行这个私有方法。

<4>通过反射来动态调用指定的方法:


import java.lang.reflect.Method;


public class RIMTest {



public static void main(String[] args) throws Exception{
Class clazz = Class.forName("com.second.Product");//生成Class对象
Product pd = (Product) clazz.newInstance();//创建Product类的对象

Method method1 = clazz.getDeclaredMethod("setName", String.class);//调用public方法,有参数
Object  returnValue = method1.invoke(pd, "爪哇");
System.out.println(returnValue);

Method method2 = clazz.getDeclaredMethod("display");//调用private方法,无参数
method2.setAccessible(true);
Object returnValue1 = method2.invoke(pd);
}


}
class Product{
private static long count = 0;//私有类成员变量
private long id ;
private String name ="无名氏";
public Product(){//构造方法
System.out.println("默认的构造方法");
id=++count;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("调用setName的方法");
System.out.println(name);
}
private void display(){
System.out.println(getClass().getName()+"[id="+id+"name="+name+"]");
}
}


3、访问成员变量的值


import java.lang.reflect.*;


public class RFTest {


public static void main(String[] args) throws Exception{
Class clazz = Class.forName("com.second.Product");
Product pd = (Product) clazz.newInstance();

Field idField = clazz.getDeclaredField("id");
idField.setAccessible(true);
idField.setLong(pd, 100);

System.out.println(idField.getLong(pd));


Field nameField = clazz.getDeclaredField("id");
nameField.setAccessible(true);
nameField.set(pd, "李四");

System.out.println(idField.get(pd));
}


}


4、JavaBean中的setXXX和getXXX方法:


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class IntroSRTest {


public static void main(String[] args) throws Exception{
ReflectPoint pt = new ReflectPoint(3,7);

String propertyName = "x";


PropertyDescriptor pd = getXXX(pt, propertyName);


int a =10;
setXXX(pt, propertyName, a);




}


private static void setXXX(ReflectPoint pt, String propertyName, int a)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,pt.getClass());
Method methodSetX = pd1.getWriteMethod();
methodSetX.invoke(pt, a);
}


private static PropertyDescriptor getXXX(ReflectPoint pt,
String propertyName) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt.getClass());

Method methodGetX = pd.getReadMethod();
Object returnValue = methodGetX.invoke(pt);
System.out.println(returnValue);
return pd;
}


}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值