首页 > 编程知识 正文

java反射的实现方式,反射的应用例子

时间:2023-05-03 08:26:35 阅读:162682 作者:2389

So from reading some of the articles,themessageigotoutofitwasbeingabletomodifyfieldsandsetvaluestoclassesinrealtimewithoutrecompoutrecompilecorecomporecompompilecleretrecled

soisitpossibletodothisto3rdpartyjavalibrarycreatedclasseswhichnosourcecodeisavailable/isitpossibletousereflectiontontomodifyclayclablable

inwhatotherscenariosisreflectioncommonlyused?

iamtryingtounderstandhowreflectioncanbeapplicable。

解决方案

anytime you ' redealingwithastringatruntimeandwanttotreatpartofthatstringasanidentifierinthelanguage。

remoteprocedurecalling---treatpartofamessagereceivedoverthenetworkasamethodname。

序列化和序列化--convertfieldnamestostringsoyoucanwritetheobject ' sfieldstoastreamandlaterconvertitbackinter

对象关系映射----maintainarelationshipbetweenfieldsinanobjectandcolumnsinadatabase。

interfaceswithdynamicallytypedscriptinglanguages-- turnastringvalueproducedbyascriptinglanguageintoareferencetoafieldormethorm

itcanalsobeusedtoallowlanguagefeaturestobeemulatedinthelanguage。

considerthecommandlinejavacom.example.myclasswhichturnsastringintoaclassname.this doesn ' trequirereflection, becausethejavaexecutablecanturna.classfileintocode,butwithouldnotbeabletowritejavacom.example.wrapper code

class Wrapper {

publicstaticvoidmain (string . argv ) throws Exception { )。

//dosomeinitializationorotherwork

class delegate=class.forname (argv [0];

method main=delegate.get method (' main ',string ) ).class );

main.apply(null,Arrays.aslist ) argv ).subList(1) 1,argv.length ).toarray ) argv );

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。