📄 ch08.htm
字号:
<HR>
<P>
<B>Listing 8.11 Simple Example of a User-Defined Data
Type </B><TT><B>CHAR2<BR>
</B></TT>
<BLOCKQUOTE>
<PRE>
1 report ztx0811.
2 types char2(2) type c.
3 data: v1 type char2 value 'AB',
4 v2 type char2 value 'CD'.
5
6 write: v1, v2.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 8.11 produces this output:
<BLOCKQUOTE>
<PRE>
AB CD<BR>
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 defines a data type named <TT>char2</TT>. It is a two-byte
<TT>char</TT> field. On lines 3 and 4, variables <TT>v1</TT> and
<TT>v2</TT> are defined as two-byte <TT>char</TT> fields using
the data type <TT>char2</TT>. They are given default values and,
on line 6, they are written out.<BR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.12 Using Types Can Make Your Code Clearer
and Easier to Read<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0812.
2 types: dollars(16) type p decimals 2,
3 lira(16) type p decimals 0. "italian lira have no
decimals
4
5 data: begin of american_sums,
6 petty_cash type dollars,
7 pay_outs type dollars,
8 lump_sums type dollars,
9 end of american_sums,
10 begin of italian_sums,
11 petty_cash type lira,
12 pay_outs type lira,
13 lump_sums type lira,
14 end of italian_sums.
15
16 american_sums-pay_outs = '9500.03'. "need quotes when literal
contains a decimal
17 italian_sums-lump_sums = 5141.
18
19 write: / american_sums-pay_outs,
20 / italian_sums-lump_sums.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 8.12 produces this output:
<BLOCKQUOTE>
<PRE>
9,500.00
5,141
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 defines a data type named <TT>dollars</TT> as a 16-byte
packed decimal field with two decimal places. Line 3 defines a
similar data type named <TT>lira</TT> with 0 decimal places. On
lines 5 through 14, two field strings are defined using the new
data types. One component of each is assigned a value on lines
16 and 17, and on lines 19 and 20 they are written out.
<P>
Think of a user-defined type as a variable, but one that you can't
use to store data. It can only be used to create other variables.
The same rules apply to types as apply to variables and field
strings. Type names, like variable names, are one to 30 characters
long, but unlike variables, their names cannot include the characters
- < >.
<H2><A NAME="StructuredTypes"><FONT SIZE=5 COLOR=#FF0000>
Structured Types</FONT></A></H2>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A user-defined type can be based on the definition of a field
string. This is known as a <I>structured type</I>. Listing 8.13
shows how you can shorten a program using a structured type.
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<HR>
<P>
<B>Listing 8.13 Using Structured Types can Reduce Redundancy
and Make Maintenance Easier<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0813.
2 types: begin of address,
3 street(25),
4 city(20),
5 region(7),
6 country(15),
7 postal_code(9),
8 end of address.
9
10 data: customer_addr type address,
11 vendor_addr type address,
12 employee_addr type address,
13 shipto_addr type address.
14
15 customer_addr-street = '101 Memory Lane'.
16 employee_addr-country = 'Transylvania'.
17
18 write: / customer_addr-street,
19 employee_addr-country.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Lines 2 through 8 define a data type named <TT>address</TT> that
contains five fields. On lines 10 through 13, four field strings
are defined using the new type. Without the new type, these definitions
would have used an additional 24 lines of code. Maintenance is
also made easier: If a change to the definitions of the address
field strings is needed, only the definition of the type needs
to be changed.
<H3><A NAME="TypeGroups">
Type Groups</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <TT>types</TT> statement can be stored in a <I>type group</I>.
A type group (also known as a <I>type pool</I>) is a Data Dictionary
object that exists merely to contain one or more <TT>types</TT>
or <TT>constants</TT> statements. Using the <TT>type-pools</TT>
statement in your program, you access types or constants from
a type group and use them in your program. Multiple programs can
share a type group, giving you the ability to create centralized
definitions. Figure 8.2 illustrates this concept. Listing 8.14
contains an example of a type group, and Listing 8.15 presents
an example of a program using <TT>types</TT> and <TT>constants</TT>
from it.
<P>
<A HREF="javascript:popUp('f8-2.gif')"><B>Figure 8.2 :</B> <I>A type group is a container for types and
constants statements. It can be shared among programs</I>.</A><BR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.14 An Example type-pool Statement Containing
Types and Constants <BR>
</B>
<BLOCKQUOTE>
<PRE>
1 type-pool ztx1.
2 types: ztx1_dollars(16) type p decimals 2,
3 ztx1_lira(16) type p decimals 0.
4 constants: ztx1_warning_threshold type i value 5000,
5 ztx1_amalgamation_date like sy-datum value '19970305'.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 1 indicates the beginning of the type group and gives it
a name. Lines 2 through 5 define <TT>types</TT> and <TT>constants</TT>
that can be used in any program.<BR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.15 Using Type Group Reduces Duplication
of Code for Easier Maintenance<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0815.
2 type-pools ztx1.
3 data: begin of american_sums,
4 petty_cash type ztx1_dollars,
5 pay_outs type ztx1_dollars,
6 lump_sums type ztx1_dollars,
7 end of american_sums.
8
9 american_sums-pay_outs = '9500.03'.
10
11 if american_sums-pay_outs > ztx1_warning_threshold.
12 write: / 'Warning', american_sums-pay_outs,
13 'exceeds threshold', ztx1_warning_threshold.
14 endif.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 includes the definitions from type group <TT>ztx1</TT>
in the program. Lines 3 through 7 define a field string using
type <TT>ztx1_dollars</TT> from the type group. Line 9 assigns
a value to <TT>pay_outs</TT>, and it is compared with constant
<TT>ztx1_warning_threshold</TT> from the type group on line 11.
<P>
If the type group has already been included, subsequent attempts
to include it are ignored and they do not cause an error.
<P>
Type group names can be one to five characters long, and must
begin with <I>y</I> or <I>z</I>. Types and constants included
in a program using the <TT>type-pools</TT> statement always have
global visibility.
<H3><A NAME="CreatingaTypeGroup">
Creating a Type Group</A></H3>
<P>
To create a type group in the Data Dictionary, use the following
procedure.
<P>
<IMG SRC="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Create a Type Group" now.
<P>
To create a type group:
<OL>
<LI>Begin at the Dictionary: Initial Screen (menu path Tools->ABAP/4
Workbench, Development->ABAP/4 Dictionary).
<LI>Type the name of your type group in the Object Name field.
<LI>Select the Type Groups radio button.
<LI>Press the Create pushbutton. The Type Group <I>xxxx</I>: Create
Text screen is displayed.
<LI>Type a description of your type group in the Short Text field.
<LI>Press the Save button. The Create Object Catalog Entry screen
is displayed.
<LI>Press the Local Object button. The ABAP/4 Editor: Edit Type
Group screen is <BR>
displayed. On the first line, the statement <TT>type-pool <I>t</I>.</TT>
appears, where <TT><I>t</I></TT>
is the name of your type group. If it does not appear, you should
type it now.
<LI>On subsequent lines, type <B>constants</B> and <B>types</B>
statements. All names must begin with <TT><I>t</I>_</TT>,
where <TT><I>t</I></TT> is the
name of your type pool.
<LI>Press the Save button on the Application toolbar. At the bottom
of the window, the message <TT>Type group saved</TT> is displayed.
<LI>Press the Back button on the Application toolbar to return
to the Dictionary: Initial Screen.
</OL>
<P>
To delete a type group, follow steps 1 through 3 of the preceding
procedure and then press the Delete button on the Application
toolbar.
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<UL>
<LI>Field strings are like structures that are defined within
your program. You can define them using either <TT>data</TT> or
<TT>tables</TT>. Field strings defined using <TT>tables</TT> have
both global and external visibility.
<LI>You can define your own data types, even structured types,
using the <TT>types</TT> statement. User-defined types reduce
redundancy and make maintenance easier.
<LI>You can define a type group in the Data Dictionary to make
your user-defined types and constants reusable.
</UL>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=288><CENTER><B>DO</B></CENTER></TD><TD WIDTH=288><CENTER><B>DON'T</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288>DO use an underscore to make your variable names easier to read.
</TD><TD WIDTH=288>DON'T use a dash in a variable name; a dash delimits the components of a field string.
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="QampA"><FONT SIZE=5 COLOR=#FF0000>
Q&A</FONT></A></H2>
<TABLE>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>Why do I need to create field strings? Why can't I use just simple variables?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>Field strings give you a way of organizing your variables into groups. When you have hundreds of variables in a program, organizing them into field strings makes the relationships between the variables clearer. Using field strings to group fields together also enables you to perform an operation on a group of variables as if they were a single variable. So, for example, if you need to move 100 variables, you can usually code a single statement to perform the move from one field string to another, instead of coding 100 assignment statements for individual variables. Moving a single field string is also more efficient than 100 move statements, so your program runs faster. You will see in the coming chapters that many statements can use a field string as an operand. If you can use field strings, you can take advantage of them, and they will save you a lot of coding and debugging time.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>Why would I use my own structured type instead of a DDIC structure? Can't I create field strings with both?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>Yes, you can. The functionality of DDIC structures far outweighs that provided by structured types because they can provide labels, F1, and F4 help. However, <TT>types</TT> can be used to define nested structured types that contain internal tables. DDIC structures can't. This will be demonstrated in <A HREF="../ch12/ch12.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch12/ch12.htm" >Chapter 12</A> in the section on internal tables.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><FONT SIZE=5 COLOR=#FF0000>
Workshop</FONT></A></H2>
<P>
The Workshop provides you two ways for you to affirm what you've
learned in this chapter. The Quiz section poses questions to help
you solidify your understanding of the material covered and the
Exercise section provides you with experience in using what you
have learned. You can find answers to the quiz questions and exercises
in Appendix B, "Answers to Quiz Questions and Exercises."
<H3><A NAME="Quiz">
Quiz</A></H3>
<OL>
<LI>What is the one pre-defined constant that can be used in an
ABAP program. What is its equivalent using a literal?
<LI>What types of type statement can be used and what type can
be defined in the Data Dictionary?
</OL>
<H3><A NAME="Exercise1">
Exercise 1</A></H3>
<P>
What is another way to declare the variable below using a "type"
declaration?
<BLOCKQUOTE>
<PRE>
data: begin of usd_amount,
hotel type p decimals 2,
rent_car type p decimals 2,
plane type p decimals 2,
food type p decimals 2,
end of usd_amount,
begin of amex_pt,
hotel type p decimals 2,
rent_car type p decimals 0,
plane type p decimals 0,
food type p decimals 0,
end of amex_pt.
</PRE>
</BLOCKQUOTE>
<H3><A NAME="Exercise2">
Exercise 2</A></H3>
<P>
What is the outcome of this program?
<BLOCKQUOTE>
<PRE>
report ztx0816.
data: begin of fld_stg1,
var1 value '1',
var2 value '3',
var3 value '5',
var4 value '7',
var5 value '9',
var6 value '4',
var7 value '8',
var8 value '6',
end of fld_stg1,
fld_stg2 like fld_stg1.
fld_stg2 = fld_stg1.
write: / fld_stg2-var1, fld_stg1-var2, fld_stg2-var6, fld_stg1-var3.
write: / fld_stg1-var5, fld_stg2-var7, fld_stg2-var4, fld_stg1-var8.
</PRE>
</BLOCKQUOTE>
<P>
<CENTER>
<HR SIZE=4>
<A HREF="gla02.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch08/gla02.htm"><IMG SRC="../button/previous.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/previous.gif" BORDER="0"></A>
<A HREF="../index.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/index.htm"><IMG SRC="../button/contents.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/contents.gif" BORDER="0"></A>
<A HREF="../ch09/ch09.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch09/ch09.htm"><IMG SRC="../button/next.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/next.gif" BORDER="0"></A>
</P>© <A HREF="../copy.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/copy.htm">Copyright</A>, Macmillan Computer Publishing. All rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -