您好,欢迎访问三七文档
ABAP351AdvancedandGenericProgramminginABAPGerdKluger,SAPAGBjörnMielenhausen,SAPAG©SAPAG2004,SAPTechEd/ABAP351/3ObjectivesAftercompletingthisworkshopyouwillbeableto:UnderstandhowtomakeyourprogramsmoreflexibleWritegenericservicesthatcanworkwitharbitrarilystructureddataDistinguishdifferentkindsofgenericityUsegenerictypestoaddflexibilityandsafetytoABAPprogramsExplaintheABAPtypesystemandtheRTTSCreateABAPtypesatruntime©SAPAG2004,SAPTechEd/ABAP351/4SessionOverviewSimpleGenericConceptsinABAPDynamicTokenSpecificationFullyGenericProgramsRTTS&DynamicTypeCreation©SAPAG2004,SAPTechEd/ABAP351/5FieldSymbolsDATA:text(20)TYPEcVALUE'Helloworld'.FIELD-SYMBOLS:fsTYPEany.ASSIGNtextTOfs.WRITE/fs.FieldsymbolsarealiasesrepresentingfieldsdynamicallyAssignmentoffieldstofieldsymbolsatruntimeNocopyingFieldsymbolsarenotpointers©SAPAG2004,SAPTechEd/ABAP351/6CastingFieldSymbolsTYPES:MY_TYPE(9)TYPEC.DATA:SmallField(5)TYPEC,LargeField(10)TYPECVALUE'1234567890',TypeName(7)TYPECVALUE'MY_TYPE',SomeTypeTYPEREFTOcl_abap_typedescr.FIELD-SYMBOLS:faTYPEANY,fsTYPEmy_type.ASSIGNLargeFieldTOfsCASTING.ASSIGNLargeFieldTOfaCASTINGTYPEMY_TYPE.ASSIGNLargeFieldTOfaCASTINGTYPEN.ASSIGNLargeFieldTOfaCASTINGTYPE(TypeName).SomeType=cl_abap_typedescr=describe_by_name('MY_TYPE‘).ASSIGNLargeFieldTOfaCASTINGTYPEHANDLESomeType.ASSIGNLargeFieldTOfaCASTINGLIKESmallField.ASSIGNLargeFieldTOfaCASTINGLIKEfa.Castingto......staticallycompletelyspecifiedtype...generictype...dynamicfieldtype...staticfieldtype...dynamicallyspecifiedtype©SAPAG2004,SAPTechEd/ABAP351/7DataReferences(1)ReferencetypeREFTOtypenameforreferencestoarbitrarydataobjectsAdatareferencevariableissetbyCREATEDATAdrefTYPE|LIKE...GETREFERENCEOFDataObjectINTOdrefAccesstodataobjectofreferenceReferenceistyped?X=dref-*.accessthecompletedataobjectY=dref-comp.accesscomponentofastructureReferenceisuntyped?(e.g.REFTODATA)ASSIGNdref-*TOf.accesscompletedataobject...ASSIGNCOMPONENT'comp'OFSTRUCTUREftofc....thenaccesscomponentifdataobjectisastructure©SAPAG2004,SAPTechEd/ABAP351/8DataReferences(2)References......aresomekindofSavePointers...canbeusedascontainersforarbitrarydataobjects,e.g.tablesofdatareferences...canbedefinedinthedatadictionary©SAPAG2004,SAPTechEd/ABAP351/9DynamicCreationofDataObjects(1)Dynamicinstantiationofdatatypesontheheap,forgenericprogrammingSyntaxCREATEDATAdrefTYPEtype|(typename).CREATEDATAdrefTYPETABLEOFtype|(typename).CREATEDATAdrefTYPEREFTOtype|(typename).CREATEDATAdrefTYPEHANDLEtypeobj.CREATEDATAdrefLIKEfield.©SAPAG2004,SAPTechEd/ABAP351/10DynamicCreationofDataObjects(2)TYPES:BEGINOFstruc,aTYPEi,bTYPEcLENGTH8,ENDOFSTRUC.DATA:drefTYPEREFTODATA,tnameTYPEstring,strTYPEstruc,intTYPEi.FIELD-SYMBOLS:intTYPEi,strTYPEstruc,fTYPEany.drefCREATEDATAdrefTYPEstruc.drefASSIGNdref-*TOstr.str36ABCstr-a=36.str-b='ABC'.drefCREATEDATAdrefLIKEint.ASSIGNdref-*TOint.int5int=5.dreftname='SFLIGHT'.CREATEDATAdrefTYPE(tname).ASSIGNdref-*TOf.fSELECTSINGLE*FROM(tname)INTOf.000AA0017©SAPAG2004,SAPTechEd/ABAP351/11GenericTypesFIELD-SYMBOLS:fs_anyTYPEANY,fs_cTYPEC.anylengthGenerictypespecificationofFieldSymbolsParameters(ofsubroutines)FORMFoo_1USINGp1TYPEANY....ENDFORM.FORMFoo_2USINGp1TYPEX....ENDFORM.©SAPAG2004,SAPTechEd/ABAP351/12ABAPTypeHierarchytypesdatatypesobjecttypeselementarydatatypesinterfacesclassescomplexdatatypesstructuretypestabletypesreferencetypesfixedlengthvariablelengthdatareferencetypesobjectreferencetypes©SAPAG2004,SAPTechEd/ABAP351/13GenericTypesSTANDARDTABLESORTEDTABLEINDEXTABLEHASHEDTABLEANYTABLEC,N,X,PCSEQUENCE,XSEQUENCE,CLIKEANY,DATASIMPLE,NUMERICFullygenericPartiallygenericGenericlengthGenerictablekindandkey©SAPAG2004,SAPTechEd/ABAP351/14Exercise:InternalTableListWriterWriteaformWRITE_TABLEwhichacceptsanyinternaltable.WritethecontentsoftheinternaltabletotheABAPlist,linebylineandfieldbyfield.DefineandfillaninternaltablewithalinetypeofyourchoicetotesttheformWRITE_TABLE.TIP:UseLOOPASSIGNINGnestedwithASSIGNCOMPONENTcompindexOFSTRUCTUREandDOlooptoaccesseachfieldofeveryline.TIP:IfyouneedmoreinformationaboutthesyntaxandthesemanticsofanABAPstatementuseOnline-HelpbypressingF1andenteringthefirstwordofthestatement.©SAPAG2004,SAPTechEd/ABAP351/15SessionOverviewSimpleGenericConceptsinABAPDynamicTokenSpecificationFullyGenericProgramsRTTS&DynamicTypeCreation©SAPAG2004,SAPTechEd/ABAP351/16DynamicTokenSpecificationABAPstatementsallowtospecifysomepartsdynamically.Generalsyntax:“(token)”wheretokenisafieldevaluatedatruntime.Rule:Thecontentsofthetokenmustbeinuppercase.Nostatictypechecksfordynamicstatements.Runtimeerrorsoccurifthecontentsofthetokenareinvalid.*StaticSORTstatementSORTitabBYcomp.*DynamicSORTstatementname='COMP'....SORTitabBY(name).©SAPAG2004,SAPTechEd/ABAP351/17DifferentTypesofDynamicTokenSpecificationThereare5typesofdynamictokenspecificationDynamicfieldspecification:ThetokencontainsthenameofafieldDynamictypespecification:ThetokencontainsatypenameDynamiccomponentspecification:Thetokencontainsthenameofacomponentofastructure
本文标题:SAP--ABAP351Advanced and Generic Programming in AB
链接地址:https://www.777doc.com/doc-12059 .html