載入類,解析類組成(用來做框架)
//反射載入類person 3種方法
//1class clazz
= class.
forname
("com.sws.peerson");
//2class clazz1
=new
person()
.getclass()
;//3
class clazz3
= person.
class
;
//反射類的建構函式並建立物件
class clazz
= class.
forname
("com.sws.peerson");
//獲取無參建構函式
constructor c1 = clazz.
getconstructor
(null)
;person p =
(person)c.
newinstance
(null)
;//--------------------
constructor c2 = clazz.
getconstructor
(string.
class);
//獲取有參建構函式
person p =
(person)c.
newinstance
("name");
//--------------------
//獲取有參建構函式
constructor c3 = clazz.
getconstructor
(string.
class
,int
.class);
person p =
(person)c.
newinstance
("name",12
);//--------------------
//獲取有參建構函式
constructor c4 = clazz.
getconstructor
(string.
class
,int
.class);
person p =
(person)c.
newinstance
("name",12
);//獲取私有建構函式,引數為list等型別
constructor c4 = clazz.
getdeclaredconstructor
(list.
class);
c4.setaccessible
(true);
//暴力反射,強制訪問私有建構函式
person p =
(person)c.
newinstance
(new
arraylist()
);
//反射建立物件另乙個方法
class clazz
= class.
forname
("com.sws.peerson");
person p =
(person)clazz.
newinstance()
;//無參建構函式
class clazz
= class.
forname
("com.sws.peerson");
person p =
(person)clazz.
newinstance()
;//--------------------
//反射類的方法
//public void aal()
method method1 = clazz.
getmethod
("aal"
,null)
;method1.
invoke
(p,null)
;//--------------------
//public void aal(string name,int password)
method method2 =
clazz.
getmethod
("aal"
,string.
class
,int
.class);
method2.
invoke
(p,"name"
,123);
//--------------------
//public class aal(string name,int password)
method method3 =
clazz.
getmethod
("aal"
,string.
class
,int
.class);
class cs
=(class[
])method3.
invoke
(p,"name"
,new
int)
;//--------------------
//private void aal(inputstream in)
method method4 =
clazz.
getdeclaredmethod
("aal"
,inputstream.
class);
method4.
setaccessible
(true);
method.
invoke
(p,new
fileinputstream
("c:\\1.txt"))
;//--------------------
//private static void aal(int num)
method method5 =
clazz.
getmethod
("aal"
,int
.class);
method5.
invoke
(null,23)
;//無需物件
//--------------------
//public static void main(string args)
method method6 =
clazz.
getmethod
("main"
,string.
class);
method6.
invoke
(null,
newobject
}); string);
class clazz
= class.
forname
("com.sws.peerson");
person p =
newperson()
;//反射字段
//--------------------
//public string name="aaa"
field f = clazz.
getfield
("name");
//獲取字段值
object value = f.
get(p)
;//獲取字段型別
class type
= f.
gettype()
;if(type.
equals
(string.
class))
//設定欄位的值
f.set
(p,"name");
system.out.
println
(p.name)
;//--------------------
//反射私有物件
//private int password = 123
field f1 =
class
.getdeclaredfield
("password");
f1.setaccessible
(true);
int password = f1.
get(p)
//--------------------
//反射私有靜態字段
//private static int age = 23;
field f2 =
class
.getdeclaredfield
("age");
f2.setaccessible
(true);
int age = f2.
get(p)
java web 學習筆記 java的集合物件
arraylist 新增集合 arraylist al new arraylist al.add 1 al.add 2 遍歷集合 iterator it al.iterator list 新增集合 list list new arraylist list.add list.add 遍歷集合 iter...
Java Web學習筆記 一
1.html hyper text markup language 超文字標記語言。html的檔案字尾名一般是 htm 或者.html 2.css定義的語法 selecter 1 http是乙個基於請求 響應模式的,無狀態的協議 request response based,stateless pr...
Java Web學習筆記 三
1.jsp執行過程 2.jsp執行詳細流程 3.在jsp中定義了和 中定義了的變數的區別,中定義的變數是全域性變數,中定義的變數是區域性的.4.jsp宣告的格式 指令碼段的形式 當伺服器將 jsp 轉換為 servlet 時,會將 jsp 中的宣告轉換為類的 成員變數,而將指令碼段轉換為方法中的區域...