1、Class對象的獲取
Java中對象可以分為兩種,一種是實例對象无蜂,一種是Class對象。Class對象是在類加載的時候生成的蒙谓,而實例對象又是基于Class對象來生成的斥季。
Java中的反射使其擁有了動態(tài)語言的屬性。在程序開發(fā)的過程中累驮,反射技術(shù)應(yīng)用的核心就是Class對象的獲取酣倾。Class對象的獲取有如下三種方法,各有各的使用場景慰照。
public class TestReflect {
public static void main(String[] args){
Class<?> clazz1 = null; //Class clazz1 = null;
Class<?> clazz2 = null; //Class clazz2 = null;
Class<?> clazz3 = null; //Class clazz3 = null;
//使用<?>或者不使用<?>兩者是沒有區(qū)別的,只不過Class支持泛型以后加上<?>會更加規(guī)范
//1琉朽、使用Class的靜態(tài)方法毒租,該方法會產(chǎn)生異常,需要進行異常處理箱叁,因為可能沒有與所傳入字符串相對應(yīng)的類
try{
Class clazz1 = Class.forName("InnerTest");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2墅垮、使用Java中類字面量class獲得Class對象
Class clazz2 = InnerTest.class;
//3、使用繼承于Object的getClass()方法耕漱,該方法是native方法
InnerTest innerTest = new InnerTest();
Class clazz3 = innerTest.getClass();
}
}
class InnerTest {
}
2算色、構(gòu)造實例對象
獲得Class對象之后,采用newInstance()
螟够,該方法的返回類型是T灾梦,是一個泛型類型。如果Class對象的類型為Class或者Class<?>妓笙,則需要強制類型轉(zhuǎn)換若河。有兩種構(gòu)造實例對象的方法:1、獲得Class對象以后調(diào)用newInstance()方法寞宫;2萧福、獲得Constructor對象以后調(diào)用newInstance()方法。實際上前者也會調(diào)用后者的方法辈赋。
import java.lang.reflect.Constructor;
public class TestReflect {
public static void main(String[] args){
//若沒有傳入類型參數(shù)鲫忍,使用newInstance()方法生成對象的時候膏燕,會有兩種情況:1、只能生成Object對象悟民;2坝辫、進行強制類型轉(zhuǎn)換;
Class<?> clazz1 = null;
//此處傳入了類型參數(shù)逾雄,使用newInstance()方法生成對象的時候阀溶,無需進行強制類型轉(zhuǎn)換;
Class<InnerTest> clazz2 = null;
clazz1 = InnerTest.class;
clazz2 = InnerTest.class;
//1鸦泳、使用newInstance()的方法返回實例對象
try{
Object test11 = clazz1.newInstance();//只能生成Object對象
InnerTest test12 = (InnerTest) clazz1.newInstance();//必須進行強制類型轉(zhuǎn)換
InnerTest test2 = clazz2.newInstance();//直接可以生成所需的類型對象
}catch (IllegalAccessException e) {
e.printStackTrace();
}catch (InstantiationException e){
e.printStackTrace();
}
//2银锻、直接獲取構(gòu)造函數(shù)
Constructor<?> c1 = null;
Constructor<InnerTest> c2 = null;
try{
c1 =clazz1.getDeclaredConstructor();
c2 = clazz2.getDeclaredConstructor();
}catch (NoSuchMethodException e) {
e.printStackTrace();
}
try{
Object innerTest11 = c1.newInstance(null);
InnerTest innerTest112 = (InnerTest)c1.newInstance(null);
InnerTest innerTest2 = c2.newInstance(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class InnerTest {
}
3、構(gòu)造方法
在獲得Class的對象以后做鹰,通過該對象獲得對應(yīng)的構(gòu)造函數(shù)击纬,如果同時存在多個構(gòu)造函數(shù)。
import java.lang.reflect.Constructor;
public class TestReflect {
public static void main(String[] args){
Class<InnerTest> clazz = null;
clazz = InnerTest.class;
Constructor<InnerTest> c1 = null;
Constructor<InnerTest> c2 = null;
Constructor<InnerTest> c3 = null;
try{
c1 = clazz.getDeclaredConstructor();
c2 = clazz.getDeclaredConstructor(int.class);
c3 = clazz.getDeclaredConstructor(int.class,int.class);
}catch (NoSuchMethodException e) {
e.printStackTrace();
}
try{
InnerTest innerTest1 = c1.newInstance();
System.out.println("innerTest1 a: " + innerTest1.a + " b: " + innerTest1.b + " c: " + innerTest1.c);
InnerTest innerTest2 = c2.newInstance(6);
System.out.println("innerTest2 a: " + innerTest2.a + " b: " + innerTest1.b + " c: " + innerTest2.c);
InnerTest innerTest3 = c3.newInstance(6,12);
System.out.println("innerTest3 a: " + innerTest3.a + " b: " + innerTest1.b + " c: " + innerTest3.c);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class InnerTest {
static int a = 2;
int b;
int c;
public InnerTest() {
}
public InnerTest(int b) {
this.b = b;
}
public InnerTest(int b, int c) {
this.b = b;
this.c = c;
}
}
4钾麸、非構(gòu)造方法
非構(gòu)造方法的獲取和使用與構(gòu)造方法有著明顯的區(qū)別:1更振、獲取非構(gòu)造方法不僅需要指定參數(shù)的類型,還需要指定方法名饭尝;2肯腕、非構(gòu)造方法的調(diào)用分為兩種情況:a),靜態(tài)方法調(diào)用钥平;b)实撒,非靜態(tài)方法調(diào)用
import java.lang.reflect.Method;
public class TestReflect {
public static void main(String[] args){
Class<InnerTest> clazz = null;
clazz = InnerTest.class;
try{
//需要指定方法名和參數(shù)類型
Method m1 = clazz.getDeclaredMethod("getA");
Method m2 = clazz.getDeclaredMethod("setA", int.class);
//調(diào)用靜態(tài)方法時候,invoke函數(shù)的第一個參數(shù)為null涉瘾;
System.out.println(m1.invoke(null));
m2.invoke(null,4);
System.out.println(m1.invoke(null));
InnerTest innerTest = new InnerTest();
Method m3 = clazz.getDeclaredMethod("getB");
Method m4 = clazz.getDeclaredMethod("setB", int.class);
//調(diào)用非靜態(tài)方法的時候知态,需要指定對象名;
System.out.println(m3.invoke(innerTest));
m4.invoke(innerTest,8);
System.out.println(m3.invoke(innerTest));
}catch (Exception e) {
e.printStackTrace();
}
}
}
class InnerTest {
static int a = 2;
int b;
public static int getA() {
return a;
}
public int getB() {
return b;
}
public static void setA(int a) {
InnerTest.a = a;
}
public void setB(int b) {
this.b = b;
}
}
5立叛、域
域的獲取相對簡單负敏,只需要傳入變量名字∶厣撸可以獲取域的值和設(shè)置域的值其做。更厲害的時候可以通過setAccessible(),進而訪問private的變量
import java.lang.reflect.Field;
public class TestReflect {
public static void main(String[] args){
Class<InnerTest> clazz = null;
clazz = InnerTest.class;
try{
Field a = clazz.getDeclaredField("a");
System.out.println("a: " + a.getInt(null));
a.setInt(null,4);
System.out.println("a: " + a.getInt(null));
InnerTest innerTest = new InnerTest();
Field b = clazz.getDeclaredField("b");
System.out.println("b: " + b.getInt(innerTest));
b.setInt(innerTest,20);
System.out.println("b: " + b.getInt(innerTest));
Field c = clazz.getDeclaredField("c");
System.out.println("accessible: " + c.isAccessible());
c.setAccessible(true);
System.out.println("accessible: " + c.isAccessible()+ " c: " +c.getInt(innerTest));
c.setInt(innerTest,40);
System.out.println("accessible: " + c.isAccessible()+ " c: " +c.getInt(innerTest));
} catch (Exception e) {
e.printStackTrace();
}
}
}
class InnerTest {
static int a = 2;
int b = 10;
private int c = 20;
}