欢迎来到天天文库
浏览记录
ID:39256212
大小:69.99 KB
页数:23页
时间:2019-06-28
《generics-tutorial 英文文献资料》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、GenericsintheJavaProgrammingLanguageGiladBrachaJuly5,2004Contents1Introduction22DefiningSimpleGenerics33GenericsandSubtyping44Wildcards54.1BoundedWildcards...........................65GenericMethods76InteroperatingwithLegacyCode106.1UsingLegacyCodeinGenericCode........
2、..........106.2ErasureandTranslation.........................126.3UsingGenericCodeinLegacyCode..................137TheFinePrint147.1AGenericClassisSharedbyallitsInvocations............147.2CastsandInstanceOf..........................147.3Arrays........................
3、..........158ClassLiteralsasRun-timeTypeTokens169MoreFunwithWildcards189.1WildcardCapture............................2010ConvertingLegacyCodetoUseGenerics2011Acknowledgements2311IntroductionJDK1.5introducesseveralextensionstotheJavaprogramminglanguage.Oneoftheseisthei
4、ntroductionofgenerics.Thistutorialisaimedatintroducingyoutogenerics.Youmaybefamiliarwithsimilarconstructsfromotherlanguages,mostnotablyC++templates.Ifso,you’llsoonseethattherearebothsimilaritiesandimportantdifferences.Ifyouarenotfamiliarwithlook-a-alikeconstructsfrome
5、lsewhere,allthebetter;youcanstartafresh,withoutunlearninganymisconceptions.Genericsallowyoutoabstractovertypes.Themostcommonexamplesarecon-tainertypes,suchasthoseintheCollectionhierarchy.Hereisatypicalusageofthatsort:ListmyIntList=newLinkedList();//1myIntList.add(newI
6、nteger(0));//2Integerx=(Integer)myIntList.iterator().next();//3Thecastonline3isslightlyannoying.Typically,theprogrammerknowswhatkindofdatahasbeenplacedintoaparticularlist.However,thecastisessential.ThecompilercanonlyguaranteethatanObjectwillbereturnedbytheiterator.Toe
7、nsuretheassignmenttoavariableoftypeIntegeristypesafe,thecastisrequired.Ofcourse,thecastnotonlyintroducesclutter.Italsointroducesthepossibilityofaruntimeerror,sincetheprogrammermightbemistaken.Whatifprogrammerscouldactuallyexpresstheirintent,andmarkalistasbeingrestrict
8、edtocontainaparticulardatatype?Thisisthecoreideabehindgenerics.Hereisaversionoftheprogramfragmentgivenaboveusinggenerics:Lis
此文档下载收益归作者所有