![源碼筆記第1天基礎加強反射properties_第1頁](http://file4.renrendoc.com/view/bb308a30f7ca19b025029afe4f2be1f5/bb308a30f7ca19b025029afe4f2be1f51.gif)
![源碼筆記第1天基礎加強反射properties_第2頁](http://file4.renrendoc.com/view/bb308a30f7ca19b025029afe4f2be1f5/bb308a30f7ca19b025029afe4f2be1f52.gif)
![源碼筆記第1天基礎加強反射properties_第3頁](http://file4.renrendoc.com/view/bb308a30f7ca19b025029afe4f2be1f5/bb308a30f7ca19b025029afe4f2be1f53.gif)
![源碼筆記第1天基礎加強反射properties_第4頁](http://file4.renrendoc.com/view/bb308a30f7ca19b025029afe4f2be1f5/bb308a30f7ca19b025029afe4f2be1f54.gif)
![源碼筆記第1天基礎加強反射properties_第5頁](http://file4.renrendoc.com/view/bb308a30f7ca19b025029afe4f2be1f5/bb308a30f7ca19b025029afe4f2be1f55.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
Junit單元測試JunitJUnitJavajavamain方法。Junit屬于第工具,一般情況下需要導入jar包,不過,多數(shù)Java開發(fā)環(huán)境已經(jīng)集成了JUnit作為單元測試工具。JunitJunitjavamain在測試類方法上添加注解@Test修飾的方法要求:publicvoid方法名(){…}test開頭,沒有@est1Junit…”publicclassJunitDemo_1{publicclassJunitDemo_1{publicvoidmyTest(){System.out.println("test");}publicvoidmyBefore(){}publicvoidmyAfter(){}測試}@est反classClass對象。Class對象解析將類的二進制數(shù)據(jù)中的符號替換為直接JJava反射機制是在運行狀態(tài)中,對于任意一個類,都能夠知道這個類的所有屬性和方java語言的反射機制。ClassClasspublicclassBean{privatepublicclassBean{privateStringid;privateString6ClassConstructorMethodFiledinstanceinvokeClass//1//1語法:類名Classclazz1=語法:變量Beanbean=newClassclazz2=//Classclazz3=添加構(gòu)造BeanpublicpublicBean(){}publicBean(Stringid,StringclassName)this.id=this.className=className;System.out.print("有參構(gòu)造:"+id);System.out.println(,className);}無參構(gòu)造publicvoidtestDefaultCons()throws//ClassbeanClass=//2Constructorconstructor=Objectbean=constructor.newInstance();}有參構(gòu)造publicvoidtestParamCons()throws//ClassbeanClass=//2Bean(StringidString =//3=/*}無參構(gòu)造,簡化版獲得實例publicvoidtestDefaultSimple()throws//ClassbeanClass=//2Objectbean=beanClass.newInstance();/*}擴展:私有構(gòu)造(反射BeanprivateprivateBean(Stringid)this.id=System.out.println("有參構(gòu)造:}getConstructor()publicvoidtestPrivateCons()throwsClassbeanClass=//2Bean(StringidString//*getConstructor()將拋異常 *getDeclaredConstructor =//3Objectbean=/*}set、get方Bean類提供gettersetterpublicStringgetId(){publicStringgetId(){returnid;}publicvoidsetId(Stringid){System.out.println("setId方法執(zhí)行:id);this.id=id;}public方publicvoidtestMethod()throws//1Classclazz=//2,相當于ObjectobjnewBean();Objectobj=clazz.newInstance();//3setId//3.1*格式:getMethod(方法名,形成列表MethodsetMethod=clazz.getMethod("setId",//3.2ObjectsetReturnObjsetMethod.invoke(obj我是參數(shù)System.System.out.println("set方法返回值: 3.1MethodgetMethod=3.2ObjectgetReturnObj=getMethod.invoke(obj);System.out.println("get方法返回值:getReturnObj);}擴展:私有方法(反射privateprivateStringshow(){return"Bean["+id+"className}getMethod()publicvoidtestPrivateMethod()throws//1Classclazz=//2獲得構(gòu)造--兩個字符串形參--Bean(Stringid,StringclassName) //3 //3privateString//*getMethod(方法名,形成列表)將拋異常 *getDeclaredMethod(方法名,形成列表MethodshowMethod=4ObjectgetReturnObj=showMethod.invoke(bean);System.out.println("show方法返回值:getReturnObj);show方法返回值:Bean[ArrayListId}擴展:main方法與執(zhí)Beanmainpublicpublicstaticvoidmain(String[]args)for(inti=0;i<args.length;i++){System.out.print(args[i]+",");}}mainpublicvoidtestMainMethod()throws//1Classclazz=//2mainpublicstaticvoidmain(String[args)MethodmainMethod=clazz.getMethod("main",String[].class); System.out.println("main方法返回值:aaa,}Bean類提供成員變量publicclassBean{privatepublicclassBean{privateStringid;privateStringpublicStringpublic字段的操publicvoidtestField()throws/*public*publicString//1Classclazz=//2,相當于ObjectobjnewBean();Objectbean=clazz.newInstance();//3操作字段,進行賦值,publicString//3.1*格式:getField(字段名FielddescriptionField=//3.2,//3.3ObjectfieldReturnObj=descriptionField.get(bean);}擴展:私有字段(反射getField()publicvoidtestPrivateField()throws//Classclazz=//2,相當于ObjectobjnewBean();Objectbean=clazz.newInstance();//3--//*getField()將拋異常 *getDeclaredFieldFieldclassNameField=//3.2classNameField.set(bean,"Bean的類名稱//3.3ObjectfieldReturnObj=classNameField.get(bean);/*}PropertiesPropertiesProperties可保存在流中或從流中加載。1、Map接口的子類,map23、它是一個可以持久化的屬性集。鍵值可以到集合中,也可以到持久化的設備(硬盤、U盤、光盤)上。鍵值的來源也可以是持久化的設備。4publicObjectsetProperty(Stringkey,Stringvalue)調(diào)用Map的方法putpublicSet<String>stringPropertyNames()publicStringgetProperty(Stringkey*PropertiespublicclassPropertiesDemo01publicstaticvoidmain(String[]args)Propertiesprop=new//prop.put(key,value); "," ", ", Set<String>keys=for(Stringkey:keys)//Stringvalue=prop.getProperty(key);System.out.println(key+"=="+value);}}}}將集合中內(nèi)容到文 需求:使用Properties集合,完成把集合內(nèi)容到IO流所對應文件中的操publicpublicclassPropertiesDemo02publicstaticvoidmain(String[]args)throwsIOExceptionPropertiesprop=new "," ", ", FileWriterout=new 到流所對應的文件prop.store(out,"save}}把指定流所對應的文件中的數(shù)據(jù),出來,保存到Propertie集合pertiespublicpublicclassPropertiesDemo03publicstaticvoidmain(String[]args)throwsIOExceptionPropertiesprop=newFileInputStreamin=new//FileReaderin=new 到集合}}Beangettersetter方法,publicpublicclassBean2privateStringid;privateStringclassName;privatePropertiesprops=newUsergettersetterpublicpublicclassUserprivateStringid;privateStringusername;privateStringpwd;publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;} publicStringtoString()return"User[id="+id+",username="+username+",pwd="pwd+}propertiesUser中publicvoidtestDemo()throws1Propertiesprop=new2FileInputStreamin=new//3,把流所對應文件中的數(shù) 到集合UseruserObj=newUser();System.out.println("封裝數(shù)據(jù)前:userObj);System.out.println("封裝數(shù)據(jù)后:結(jié)果封裝數(shù)據(jù)前:User[id=nullusername=null封裝數(shù)據(jù)后:User[id=u001username=jack}propertiesUser中publicvoidtestDemo2()throws1Propertiesprop=new2FileInputStreamin=new//3,把流所對應文件中的數(shù) 到集合//UseruserObj=newClassclazz=Class.forName("com.itheima_00_Bean.User");ObjectuserObj=clazz.newInstance();System.out.println("封裝數(shù)據(jù)前:Set<String>keySet=for(Stringkey:keySet)Stringvalue=StringmethodName="set"+key.substring(0,1).toUpperCase()+Methodmethod=clazz.getMethod(methodName,String.class);method.invoke(userObj,value);}System.out.println("封裝數(shù)據(jù)后:封裝數(shù)據(jù)前:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年稅務工作者工作總結(jié)范文(3篇)
- 2024-2025學年廣東省清遠市八校聯(lián)盟高一上學期教學質(zhì)量檢測(二)歷史試卷
- 2025年企業(yè)文化建設策劃咨詢協(xié)議
- 2025年企業(yè)數(shù)據(jù)保密共享協(xié)議
- 2025年基礎設施建設項目合同律師服務協(xié)議
- 2025年公司員工協(xié)議范本
- 2025年設備采購租賃合同協(xié)議范本
- 2025年裂隙燈顯微鏡項目立項申請報告模板
- 2025年醫(yī)藥產(chǎn)品銷售合同樣本
- 2025年頻率測量儀器項目立項申請報告模板
- 20級大學物理(下)A卷期終試卷及答案解析-南京理工大學
- 自動化生產(chǎn)線運行與維護完整版課件(全)
- 人教版八年級人文地理下冊知識點整理(2021版)
- 地震應急預案及應急演練腳本
- 中國經(jīng)濟轉(zhuǎn)型導論-政府與市場的關(guān)系課件
- 二十四節(jié)氣文化融入幼兒園食育的有效途徑
- 統(tǒng)計過程控制SPC培訓資料
- 食品經(jīng)營操作流程圖
- 新視野大學英語讀寫教程 第三版 Book 2 unit 8 教案 講稿
- 小學生必背古詩詞80首硬筆書法字帖
- X52K銑床參數(shù)
評論
0/150
提交評論