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'.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值