Omitting the Declaration of TYPE POOL Statements
6.4以后,系统的type-pools声明的将在全局使用,不需要再引入TYPE- POOLS: abap, icon.
定义变量时,我们也可以参考全局Class中的公有属性定义
DATA : monster_table TYPE zcl_monste rs=>mtt_monster_table .
zcl_monsters ->make_monsters_dance( monster_table ) .
Omitting Data Type Declarations
以前变量必须在DATA中声明然后再使用
DATA: monster_instructions TYPE string.
monster_instructions = 'Jump up and down and howl'.
编译器在检查语法时,已经知道等号后面的类型,那么我们可以让编译器来决定等号前面的变量的类型,如下所示来定义变量。
DATA(monster_instructions_new) = 'Jump up and down and howl'.
DATA(number_of_monsters_new) = lines( table_of_monsters ). "table_of_monsters 已经定义
Creating Objects Using NEW
以前我们都是先定义再赋值,现在可以用New关键跟变量类型来创建对象,括号中传入相应的参数。
* Instead of
DATA: monster TYPE REF TO zcl_monster.
CREATE OBJECT monster EXPORTING name = 'FRED'.
* You'll use
DATA(monster2) = NEW zcl_monster( name = 'FRED' ).
*and instead of
DATA: sanity TYPE zde_monster_sanity.
sanity = 0.
* You'll use
DATA(sanity2) = NEW zde_monster_sanity( 1 ).
Filling Structures and Internal Tables While Creating Them Using VALUE
通常VALUE的用法像这样DATA : monster_name TYPE string VALUE ' FRED '.
,但是当定义成结构内表时,以前我们不能直接用value赋值
monster_type_selection_option-option = 'EQ'.
monster_type_selection_option-sign = 'I'.
monster_type_selection_option-low = 'MSTR'."Standard Monster
APPEND monster_type_selection_option TO monster_type_range.
DATA(monster) = NEW zcl_monster( ).
DATA(table_of_monsters) = monster->get_monsters( monster_type_range ).
7.4以后,我们可以用如下代码
TYPES: l_typ_monster_type TYPE RANGE OF zde_monster_type.
DATA(table_of_monsters2) = monster->get_monsters(
VALUE l_typ_monster_type(
( option = 'EQ' )
( sign = 'I' )
( low = 'MSTR' ) ) ). "Standard Monster
Filling Internal Tables from Other Tables Using FOR
插入内表数据可以用下面方法。
table_of_monsters = VALUE #(
( name = 'FRED' ) ( monster_number = 1 )
( name = 'HUBERT') ( monster_number = 2 ) ).
DATA(http_name_value_pair) = VALUE ihttpnvp(
name = 'Access-Control-Allow-Origin'
value = '*' ).
但通常我们是将一个内表中的值赋到另一个内表,如果结构相同可以用APPEND LINES OF green_monsters TO all_monsters .
7.4后可以用FOR循环,添加更多的限制条件
SELECT *
FROM ztsm_monsters
INTO TABLE @DATA(all_monsters).
TYPES: typ_monsters TYPE STANDARD TABLE OF ztsm_monsters WITH DEFAULT KEY.
DATA(neurotic_monsters) = VALUE typ_monsters(
FOR monster_details IN all_monsters WHERE ( sanity < 20 )
( name = monster_details-name
monster_number = monster_details-monster_number ) ).
Creating Short-lived Variables Using LET
7.4后可以用LET定义短生命周期的变量,如下在DO循环中,用LET定义了weapon_name,nonster_name,当这条语句执行后,这些变量将不再存在。
SELECT *
FROM ztsm_monsters
INTO TABLE @DATA(table_of_monsters).
DATA(iterator) = NEW lcl_monster_iterator( ).
DO lines( table_of_monsters[] ) TIMES.
DATA(weapon_description) = CONV string(
LET weapon_name = iterator->get_next_weapon( )
monster_name = table_of_monsters[ sy-index ]-name
date_string = |{ sy-datum+6(2) } / { sy-datum+4(2) } / { sy-datum(4) }|
IN |Monster { monster_name } was issued a { weapon_name } on { date_string }| ).
WRITE:/ weapon_description.
ENDDO.
String Processing
7.02前,拼接字符串用concatenate的方法,7.02后,用||来表示字符串,用{}来引入传入的变量。
DATA: helper TYPE string,
result TYPE string,
number TYPE zde_monster_number VALUE '0000000001',
status TYPE string VALUE 'is feeling GROOVY, man'.
CONCATENATE 'Monster Number' number INTO helper SEPARATED BY space.
CONCATENATE helper status INTO result SEPARATED BY ' / '.
result = |Monster Number { number } / { status }|.
7.4以前,对于ALPHA类型的前导0,要显示时,首先要CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
,要用select从数据库表查询时,首先要CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
进行转换。
7.4后,当我们要在显示string中去掉前导0,可以用message = |{ monster_number ALPHA = OUT }|.
Method Chaining
从7.02开始就可以链式地调用方法,以前先定义好了参数,再将参数传进去,现在参数可以直接传方法的返回值。
DATA: exception TYPE REF TO zcx_monster_exceptions.
TRY.
"Do Something
CATCH zcx_monster_exceptions INTO exception.
DATA(helper) = exception->get_text( ).
zcl_bc_screen_message=>output( helper ).
ENDTRY.
TRY.
"Do Something
CATCH zcx_monster_exceptions INTO exception.
zcl_bc_screen_message=>output( exception->get_text( ) ).
ENDTRY.
Avoiding Type Mismatch Dumps When Calling Functions
在CALL方法传参数时,我们要保证输入输出参数的类型与方法的参数类型一致,如果参数多就需要我们多次进入该方法查看参数类型,这样操作比较麻烦,如下面代码:
DATA: number_of_heads TYPE i,
number_of_hats TYPE i,
monster_number TYPE zde_monster_number VALUE '0000000001'.
DATA(monster) = NEW zcl_monster( ).
monster->get_ahead_get_a_hat(
EXPORTING id_monster_number = monster_number
IMPORTING ed_number_of_heads = number_of_heads
ed_number_of_hats = number_of_hats ).
7.4以后,我们可以从方法中定义变量类型而不是在程序开始时,如下代码,直接在call方法时定义变量,这样避免了类型不匹配问题,代码量也减少了,并且如果方法中参数类型修改了,此处调用也能自动匹配新类型。
monster->get_ahead_get_a_hat(
EXPORTING id_monster_number = monster_number
IMPORTING ed_number_of_heads = DATA(number_of_heads2)
ed_number_of_hats = DATA(number_of_hats2) ).
当通过构造方法定义类时,以前我们定义class具体的类型,方法返回可能子类或父类,现在我们可以通过构造方法返回的类型来决定class的类型。
DATA: monster2 TYPE REF TO zcl_green_monster.
monster2 = zcl_laboratory=>build_new_monster( ).
DATA(monster3) = zcl_laboratory=>build_new_monster( ).
Using Constructor Operators to Convert Strings
对于string类型与char类型,以前我们在参数中若类型不匹配,需要用=赋值相同类型的变量再传入方法参数。7.4开始,可以用CONV #( monster_name2 )
进行类型的转换匹配。
DATA: helper TYPE char20,
monster_name TYPE string.
monster_name = 'HUBERT'.
helper = monster_name.
DATA(monster) = NEW zcl_monster( ).
monster->invite_to_party( helper ).
DATA: monster_name2 TYPE string.
monster_name2 = 'HUBERT'.
monster->invite_to_party( id_name = CONV #( monster_name2 ) ).
Functions That Expect TYPE REF TO DATA
当我们要动态地传入参数时,将方法中的参数定义成DATA类型。7.4以前,首先create data参照传入的动态类型,然后用GET REFERENCE OF取得值再作为参数传入。7.4以后,可以直接用REF #( value )
作为动态参数传入。
DATA: generic_data_object TYPE REF TO data.
DATA(monster) = NEW zcl_monster( ).
"VALUE is an IMPORTING parameter TYPE ANY
CREATE DATA generic_data_object LIKE value.
GET REFERENCE OF value INTO generic_data_object.
monster->log( generic_data_object ).
" Using Constructor Operator REF to Fill a TYPE REF TO DATA Parameter
monster->log( REF #( value ) ).