資源描述:
《談?wù)刯ava 反射機(jī)制(談?wù)刯ava 反射機(jī)制)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、談?wù)刯ava反射機(jī)制(談?wù)刯ava反射機(jī)制).txt38aboutthejavareflectionmechanismwhenthecloudscoveredthesky,thepessimistseesthe"darkpressuretodestroy"theoptimistseeis"energy-savingLinkai".Inanyadversity,aslongasyoukeepoptimistic,youcanalwaysfindsuchastrangestrawberry.Person,p=new,Person();Whatisthis?Ofcourseis
2、toinstantiateanobject.Buttheobjecthasaproblem,thatistoknowtheclassnamecaninstantiateitsobject,sothatweintheapplicationwillbelimited.Sothereisnosuchawaythatwedon'tknowwhatthisclasscanbeinstantiateditistheobject?ThankGoodness!Fortunately,weusedjava,Javaprovidesthismechanism.1)the.Javaprogra
3、mcanobtainbytecodeinformationforanyclassatruntime,includingclassmodifiers(public,static,etc.),baseclasses(superclass,superclass),interfacesimplemented,fields,methods,andsoon2).Javaprogramsatruntimecancreateobjectinstancesoftheclass,changethefieldcontentoftheobject,andcalltheobjectmethodba
4、sedonbytecodeinformationThemechanismiscalledreflectiontechnology.Soyoucanimaginethemirror,aswelookinthemirror,themirrorandtheemergenceofa(analogymaynotbeappropriate,butenoughtoexpressmeaning).Thereflectiontechniqueprovidesageneraldynamiclinkercomponentmethodisnotnecessarytotargethardencod
5、ingtheproceduresrequiredtosourceprogram,sothatwecancreateaflexibleprogram.ThereflectionmechanismofJavaisimplementedbyreflectionAPI,whichallowstheprogramtoobtaininternalinformationofanyclasswithagivennameduringtherun.ThereflectionAPIisinthejava.lang.reflectpackage.Themaincategoriesareasfol
6、lows:1).Constructorclass:amethodfordescribingtheconstructionofaclass2).Fieldclass:amembervariableusedtodescribeaclass3).Methodclass:amethodusedtodescribeaclass4).Modiferclass:amodifierusedtodescribeelementswithinaclass5).Array:isusedtomanipulatearraysConstructor,Field,Methodofthethreeclas
7、sesareJVM(virtualmachine)createdatruntime,usedtoexpressthecorrespondingloadingmemberclass.Thethreeclassimplementsthejava.lang.reflect.Memberinterface,Memberinterfacedefinesinformationaccessclassmembersorconstructionmethods.TousethereflectionAPI,mustfirstinstancetoma