资源描述:
《visualc#2005程序设计基础教程ch09》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、VisualC#2005程序设计基础教程ch09Inthispaper,bywtgl988718contributionPowerpointdocumentsmaybeapoorexperienceattheWAPend・ItisrecommendedthatyouchooseTXT,ordownloadthesourcefiletothenativeview.C#programmingtutorialdesigntutorialAutoCADcomputeraideddesignTsinghuauniversitypressChapter9
2、:genericprogrammingThemainpointsofthischapter:Genericsaregenerics,generics,generics,andgenericsC#programmingtutorialdesigntutorialAutoCADcomputeraideddesignTsinghuauniversitypressGenericoverview9.1.1genericsintroducedDeveloperstowriteprograms,oftenmeetwiththefunctionofthetw
3、omodulesareverysimilar,justdealwithdifferentdatatypes,suchasoneisdealingwithanintdata,anotheristoprocessthestringdata,orothercustomdatatypes,forthiskindofcircumstance,canwriteseveralsimilarmethodstodealwitheachnumbetrespectivelyAccordingtothetype,justmethodindifferentparame
4、tertypes;Inc#,youcanalsodefinethetypeofdatathatisstoredasObjecttype,sothatyoucanimplementtheaboverequirementsbyboxingandunpacking.C#alsoprovidesamoreappropriategenericmechanismtoaddressthisproblem・C#programmingtutorialdesigntutorialAutoCADcomputeraideddesignTsinghuauniversi
5、typressGenericoverview9.1.1whatisgenericsTypesecurityclassescanbedefinedbygenericswithoutcompromisingtypesecurity,performance,orproductivity・Thedeveloperonlyhastoimplementtheserverasagenericserveratonce,andcanuseanytypeofsoundanduseit.Todothis,youneedtousethe〃〈“andparenthes
6、estoenclosethegeneraltypeparameters・Thegenericformatisasfollows:PublicclassStack{privateT[]PublicTPop(){・・・}publicvoidPush(Titem){•・・(intI){this・data二newT[I]}}C#programmingtutorialdesigntutorialAutoCADcomputeraideddesignTsinghuauniversitypressGenericoverview9.1.2whatisge
7、nericsGenericclassesareinthesameformatasthegenericclasses,justintroducinggenericdatatypeTthatcanbeappliedtoanydatatypeandtypesafe・Thegenericclass,sinvocationmethodisasfollows://instantiatetheclassobjectStacka二newStack(100)oftheinttypedata.Amypolumboush(10);//thisrowisnotcom
8、piledbecauseclassareceivesonlyinttypedataa.Push(“8888“);//notypeconversionofintx=a