Field symbols allow you to access data objects dynamically in ABAP programs. Unlike static access to a data object, where you need to specify the name of the object, field symbols allow you to access and pass data objects whose
name and attributes you do not know until runtime.
You can regard a field symbol as a symbolic name for a data object. Field symbols use value semantics. When you address a field symbol, the system works with the contents of the data object assigned to it, and not with the contents of the field symbol itself.
说了这么多,就是 指针和动态编译。。。。。。
在网上找了两个比较容易理解的帖子如下,要看全的就是BC - ABAP Programming书里的200-220页很详细 很详细:
1----------------------
FIELD-SYMBOLS: <fs> [<type>]为内部字段定义field-symbols .
当不输入<type>时,<fs>继承赋给它的变量的所有属性
当输入<type>时,赋给它的变量必须与<fs>同类型
例如: (说明:用户日期格式为dd.mm.yyyy)
FIELD-SYMBOLS: <f1> .
DATA: dat(8) VALUE '19950916' .
ASSIGN dat TO <f1> .
WRITE:/ <f1> .
<f1>继承dat属性,为C型,输出为: 19950916
FIELD-SYMBOLS: <f1> TYPE d.
DATA: dat(8) VALUE '19950916' .
ASSIGN dat TO <f1> .
WRITE:/ <f1> .
<f1>类型为D型,而dat为C型,类型不匹配,出现语法错误
2-----------------------
Field-Symbols:
指向一个内存区,可以通过地址访问指定内存区的数据, 就像指针一样
实例:
DATA:
var(10) TYPE c.
FIELD-SYMBOLS <fs> TYPE ANY. "定义<fs>指向任何类型,不能指向表类型
var = 'Hello'.
ASSIGN var TO <fs>.
WRITE: / <fs>.
输出:
Hello
更改字段数据:
<fs> = 'World'.
WRITE:/ var.
输出:
World
<fs>是一个指向变量var的指针,操作<fs>就身操作var本身一样。
field-symbols一般操作:
DATA:
BEGIN OF struc,
comp1 TYPE c LENGTH 1 VALUE '1',
comp2 TYPE c LENGTH 20 VALUE 'abcdefg',
END OF struc,
comp_value TYPE c LENGTH 10 VALUE 'value',
comp_name TYPE c LENGTH 15.
FIELD-SYMBOLS <fs> TYPE ANY.
"指派结构的一个字段给<fs>
ASSIGN struc-comp1 TO <fs>. WRITE:/ <fs>. " 输出:1
"指派结构字段的基地址加上两倍此字段长度的地址给字段符号
ASSIGN struc-comp1 INCREMENT 2 TO <fs>. WRITE:/ <fs>. " 输出: b
"指派结构的第二个组件字段给字段符号
ASSIGN COMPONENT 2 OF STRUCTURE struc TO <fs>. WRITE:/ <fs>. " 输出:abcdefg
"动态指定字段名
comp_name = 'comp_value'.
ASSIGN (comp_name) TO <fs>. WRITE:/ <fs>. "输出:value
通过字段符号与动态字段名称的定义(comp_name),可以在ABAP运行时动态的获取设定变量的数据, 增强程序的灵活性。