一、Class類
要想使用反射,首先需要獲得該類或者該對象所對應的Class對象。常用的三種方式:
- 使用Class的靜態方法forName:
Class<?> classType = Class.forName("java.lang.String");
2)使用類的.class方法:
Class<?> classType = String.class;
3)使用對象的getClass方法:
String s = "hello"; Class<?> classType = s.getClass();
二、Constructor類
1、 如果想通過類的不帶參數的構造方法來生成對象,有兩種放式:
1)先獲得Class對象,然后通過該Class對象的newInstance()方法直接生成即可:
Class<?> classType = String.class;
Object obj = classType.newInstance();
2)先獲得Clsss對象,然后通過該Class對象獲得對應的Constructor對象,再通過Constructor對象的newInstance()方法生成:
Class<?> classType = String.class;
Constructor cons = classType.getConstructor(new Class[] {});
Object obj = cons.newInstance(new Object[]{});
2、如果想通過類的帶參數的構造方法來生成對象,只能通過下面一種方式:
Class<?> classType = String.class;
Constructor cons = classType.getConstructor(new Class[] {String.class, int.class});
Object obj = cons.newInstance(new Object[]{"hello", 3});
三、Method類
1、獲取某類下的公有或全部方法
//獲得類的public類型的方法
Method[] methods = classType.getMethods();
//獲得類的所有方法
Method[] methods2 = classType.getDeclaredMethods();
2、獲取某類下的一個具體方法
//得到add方法對應的Method,第一個參數是方法的名字,第二個參數是該方法參數類型的一個數組
Method addMethod = classType.getMethod("add", new Class[] {int.class, int.class});
四、Field類
1、獲取某類下的公有或全部成員變量
Class<?> classType = String.class;
//獲得對象的所有public成員變量
Field[] fields2 = classType.getFields();
//獲得對象的所有成員變量
Field[] fields = classType.getDeclaredFields();
2、獲取某類下一個具體的成員變量
//獲得該類下的名為name的成員變量
Field field = classType.getField("name");
- 一個使用Class、Method、Constructor、Filed類的Demo
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
*使用Class、Method、Constructor、Filed類的Demo
*/
public class ReflectTester
{
/*
* copy(Object object)方法,這個方法能夠創建一個和參數object 同樣類型的對象,
* 然后把object對象中的所有屬性拷貝到新建的對象中,并將它返回
*/
public Object copy(Object object) throws Exception
{
//傳入參數是一個對象,所以用getClass()得到對應的Class
Class<?> classType = object.getClass();
//新建一個與object同類的對象,通過無參數構造方法
Object objectCopy = classType.getConstructor(new Class[]{})
.newInstance(new Object[] {});
//獲得對象的所有public成員變量
Field[] fields2 = classType.getFields();
//獲得對象的所有成員變量
Field[] fields = classType.getDeclaredFields();
for(Field field : fields)
{
String name = field.getName();
//獲得該成員變量的首字母,并將其轉化為大寫
String firstLetter = name.substring(0, 1).toUpperCase();
//得到該成員變量的get和set方法的名字
String getMethodName = "get" + firstLetter + name.substring(1);
String setMethodName = "set" + firstLetter + name.substring(1);
//得到該成員變量的get和set方法
Method getMethod = classType.getMethod(getMethodName, new Class[] {});
Method setMethod = classType.getMethod(setMethodName, new Class[] {field.getType()});
//得到傳入對象object的該成員變量的值
Object value = getMethod.invoke(object, new Object[] {});
//將value拷貝給objectCopy
setMethod.invoke(objectCopy, new Object[] {value});
}
return objectCopy;
}
public static void main(String[] args) throws Exception
{
Customer customer = new Customer("Tom", 20);
//由于id為Long類型,所以在傳入參數的時候應該在數字后面加L
customer.setId(1L);
ReflectTester tester = new ReflectTester();
//實現拷貝
Customer customerCopy = (Customer)tester.copy(customer);
System.out.println(customerCopy.getId() + "," + customer.getName() +
"," + customer.getAge());
}
}
五、注意點
1、Integer.TYPE返回的是int,Integer.class返回的是Integer對應的Class。