獲取執行時類的屬性
public class testfields獲取執行時類的方法//2、getdeclaredfields():能夠獲取執行時類本身中宣告的所有屬性
field fields1 = clazz.getdeclaredfields();
for (field f:fields1)
}/**
* 許可權修飾符,變數型別,變數名
* 獲取屬性的各個部分的內容
*/@test
public void
test2()
}}
public class testmethods獲取執行時類的構造器system.out
.println();
//2、getdeclaredmethods():獲取執行時類本身宣告的方法
method methods2 = clazz.getdeclaredmethods();
for (method m:methods2)
}/**
* 註解、許可權修飾符、返回值型別、方法名、形參列表、異常
*/@test
public void
test2()
//2、許可權修飾符
int i = m.getmodifiers();
string str = modifier.tostring(i);
system.out
.println(str);
//3、返回值型別
class returntype = m.getreturntype();
system.out
.println(returntype.getname());
//4、方法名
system.out
.println(m.getname()+" ");
//5、形參列表
class parameters = m.getparametertypes();
for (class c:parameters)
//6、異常型別
class exceptiontypes = m.getexceptiontypes();
for (class excep:exceptiontypes) }}
}
public class testconstructors其他//2、getdeclaredconstructors():獲取執行時類本身宣告的方法
constructor constructors2 = clazz.getdeclaredconstructors();
for (constructor c:constructors2)
}}
public class testothers}/**
* 獲取實現介面
*/@test
public void
test2()
}/**
* 獲取所在的包
*/@test
public void
test3()
/*** 獲取註解
*/@test
public void
test4()
}}
反射獲取執行時類的完整結構
field method constructor superclass inte ce annotation 1 實現的全部介面 2 所繼承的父類 3 全部的構造器 4 全部的方法 5 全部的field public class getinte ces 確定此物件所表示的類或介面實現的介面。publ...
通過反射的方式獲取類物件的屬性
public class searchproperty catch classnotfoundexception nosuchfieldexception illegalacces ception nosuchmethodexception e catch invocationtargetexcep...
java 如何通過反射獲取泛型類
核心 編寫在父類中 public class basedaoimplimplements basedao 省略具體的操作.應用場景 目標 為了實現 的重用性,編寫了父類介面basedao和父類方法basedaoimpl,想用basedaoimpl類實現運算元據庫共有的增刪改查。問題 因此父類方法必須...