sap 7.40 新特性 02- Instantiation Operator NEW

With Release 7.40 ABAP supports so called constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax for constructor expressions is

… operator type( … ) …

operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be dreived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be specified.

Instantiation Operator NEW

The instantiation operator NEW is a constructor operator that creates an object (anonymous data object or instance of a class).

  • … NEW dtype( value ) …

creates an anonymous data object of data type dtype and passes a value to the created object. The value construction capabilities cover structures and internal tables (same as those of the VALUE operator).

  • … NEW class( p1 = a1 p2 = a2 … ) …

creates an instance of class class and passes parameters to the instance constructor.

  • … NEW #( … ) …

creates either an anonymous data object or an instance of a class depending on the operand type.

You can write a compnent selector -> directly behind NEW type( … ).

Example for data objects

 

Before Release 7.40

FIELD-SYMBOLS <fS> TYPE data.

DATA dref TYPE REF TO data.

CREATE DATA dref TYPE i.

ASSIGN dref->* TO <fs>.

<fs> = 555.

With Release 7.40

DATA dref TYPE REF TO data.

dref = NEW i( 555 ).

Example for instances of classes

Before Release 7.40

DATA oref TYPE REF TO class.

CREATE OBJECT oref EXPORTING …

With Release 7.40

Either

DATA oref TYPE REF TO class.

oref = NEW #( … ).

or with an inline declaration

DATA(oref) = NEW class( … ).

TYPES: BEGIN OF t_struct1,
         col1 TYPE i,
         col2 TYPE i,
       END OF t_struct1,
       BEGIN OF t_struct2,
         col1 TYPE i,
         col2 TYPE t_struct1,
         col3 TYPE TABLE OF t_struct1 WITH EMPTY KEY,
       END OF t_struct2,
       t_itab TYPE TABLE OF t_struct2 WITH EMPTY KEY.

DATA(dref) =
  NEW t_itab( ( col1 = 1
                col2-col1 = 1
                col2-col2 = 2
                col3 = VALUE #( ( col1 = 1 col2 = 2 )
                                ( col1 = 3 col2 = 4 ) ) )
              ( col1 = 2
                col2-col1 = 2
                col2-col2 = 4
                col3 = VALUE #( ( col1 = 2 col2 = 4 )
                                ( col1 = 6 col2 = 8 ) ) ) ).
WRITE: 'abc'.

猜你喜欢

转载自blog.csdn.net/qq_16116183/article/details/81713636