Más contenido relacionado
La actualidad más candente (20)
Abap oo
- 2. 一 . 介绍
传统的 ABAP 面向过程编程
Procedural Programming
Functions are defined independently
Data Data Data
of data structures
Data Data Direct access to data
Function
Function
Function
Function Function
Function Function
Function
Function Function
Function Function Function
Function Function
Function
© SAP AG 1999
- 3. 面向对象编程
Object-Oriented Programming Model
lcl_class
Class
Attribute
Gives a general description of objects
(“blueprint”) Attribute
Establishes status types (attributes) and
behavior (methods) Method
Method
Object
Reflection of real world Method
Data
Method
Specific instance of a class
© SAP AG 1999
- 4. ABAP Objects 是 ABAP 的扩展,集合了 Java , C ++,
Smalltalk 等语言的特点。和原来的 ABAP 无缝集成。
History of Programming Languages
Machine language
Assembler
ABAP
C++
Java
ABAP Objects
© SAP AG 1999
- 5. 二 . 主要概念
Class
在 SAP 中,可以在 SE24 或 ABAP 编辑器中定义
Class 。二者的区别类似 SE11 中定义的结构与
程序中定义的结构。
在 SE24 中,可以看到类的各个部件,条理清楚。
代码中的关键字,如 public , class-method 等
在 SE24 中都可以体现出来。
- 6. 在 ABAP 编辑器中定义 Class
Class :分两部分定义。 Class 中不能再嵌套定义
Class 。
The Class as a Blueprint for Objects
Definition part
CLASS <classname> DEFINITION.
The class components (for
components
ENDCLASS. example, attributes and methods)
attributes methods)
are defined in this part.
CLASS <classname> IMPLEMENTATION. Implementation part
Implementation part
This part only contains the method
part contains the method
ENDCLASS.
implementations.
© SAP AG 1999
- 8. Objects
Object :就是 class 的实例
( instance )
使用 object 前,先要用下面的语句来定义
该 object 属于哪个类
data <object name> type ref to <class name>
定义完该对象后,就可以使用 create object
<object name> 来创建对象了。这时系统会
分配一块内存区域给对象。
- 9. create object 时,在内存中分配一个空间给该
Object 。
Creating Objects: Syntax
DATA: airplane1 TYPE REF TO lcl_airplane,
CREATE OBJECT <reference>. airplane2 TYPE REF TO lcl_airplane.
CREATE OBJECT airplane1.
CREATE OBJECT airplane2.
airplane1
name:
weight: 0
airplane2 name:
weight: 0
Main memory
© SAP AG 1999
- 10. 如果内存中某个 object 区域不被指向,系统将释放这部
分内存。下图中对两个 object 使用等号之后,它们指向
同一个内存地址,同时释放一个 object 所占用的内存。
Garbage Collector
...
DATA: airplane1 TYPE REF TO lcl_airplane,
airplane2 TYPE REF TO lcl_airplane.
CREATE OBJECT airplane1 EXPORTING ... .
CREATE OBJECT airplane2 EXPORTING ... .
airplane1 = airplane2.
airplane1
name: LH B
weight: 30,000 kg
airplane2 name: AA Bost
weight: 45,000 kg
Main memory
© SAP AG 1999
- 11. 可以使用 TYPE TABLE OF REF TO <class> 关键字定义一个内表,
来存放该类的多个对象。
- 12. Attribute
可以是各种类型,包括:
Data types: scalar ( 例如 data element),
structured, in tables ,
ABAP elementary types (C, I, ...)
Object references
Interface references
- 13. 按定义位置,分为 Public 和 Private.
Public Attribute :能从 class 外直接访问和修
改。一般来说,尽量少定义 Public
Attribute 。
Private Attribute :只能从类中访问和修改。
- 14. 按定义的关键字,分为 Instance 和 Static.
Instance Attribute :同一个类中多个实例的 Instance
Attribute 各自分开,具有不同的值。使用 DATA 关键字
定义。
用法:实例名 -> 属性
Static Attribute :同一个类中多个实例的 Static
Attribute 的值一样。可以通过各个实例或直接通过类来
访问和修改。使用 CLASS-DATA 定义。在 debug 时可
以看到 class-data 不占用 object 的内存。
用法:实例名 -> 属性
或 类名 => 属性
- 15. Method
按定义位置,分为 Public 和 Private. 类
似 Attribute 。
Public Method : 能从类外调用。
Private Method :只能从该类中调用。
- 16. 按定义的关键字,分为 Instance 和
Static.
Instance Method :能使用 static 和 instance 的内容。
使用 Method 定义。
用法:实例名 -> 方法名
Static Method :只能使用 static 的内容。可以不通过实
例,直接调用。使用 Class-Method 定义。
用法:实例名 -> 方法名
或 类名 => 方法名
- 17. Method 的参数
Method 的参数可以是 Class/Object
可以有 importing, exporting,
changing, returning 等
- 18. Constructor 方法(初始化)
每个类都自动隐性带有名为 constructor
( Instance )和
class_constructor ( Static )方法。
要使用这两个 method ,必需首先定义它
们。 constructor 可以有参数和例外,而
class_constructor 没有参数。
- 19. 每次使用 create object 时自动执行
constructor 方法 .
Class-constructor 在一个程序中只执行
一次。在以下事件前自动执行。
CREATE OBJECT
调用该类的属性
使用 Call Method
Registering a static event handler
Registering an evetm handler method for a static
event
- 20. 如果 constructor 带有参数,在 create
object 时就能够通过传入的参数初始化
object 。
例如
CREATE OBJECT airplane1 exporting
p2_name = 'Hansemand'
p2_planetype = 'Boing 747'.
- 21. Protected
protected sector 中的内容用可以被该类
自己,其父类和子类访问。
- 22. 继承
可以有多级继承
一个子类只有一个直接父类
类知道自己的 superclass ,但不知道自己
的 subclass
继承时, superclass 的内容不能除去,只
能加入新的内容
用法:定义 class 时,使用关键字
INHERITING FROM
- 23. Inheritance and Visibility
CLASS lcl_airplane DEFINITION.
Public components PUBLIC SECTION.
Visible to all METHODS get_name RETURNING
VALUE(re_name) TYPE string.
Direct access
Protected components PROTECTED SECTION.
DATA tank TYPE REF TO lcl_tank.
Only visible within their
class and within the PRIVATE SECTION.
subclass DATA name TYPE string.
Private components
ENDCLASS.
Only visible within the class
No access from outside the lcl_airplane + public
class,not even from the # protected
subclass # tank : lcl_tank
- private
- name : string
+ get_name ( ) : string
© SAP AG 1999
- 24. Interface
Defining and Implementing an Interface
INTERFACE lif_document.
Interface only has a
DATA: author TYPE REF TO lcl_author.
declaration METHODS: print,
display.
An interface ENDINTERFACE.
corresponds to an
abstract class that CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.
only contains INTERFACES lif_document.
abstract methods METHODS: display.
ENDCLASS.
Interfaces are
implemented in CLASS lcl_text_document IMPLEMENTATION.
classes METHOD lif_document~print.
ENDMETHOD.
Interfaces do not METHOD lif_document~display.
have visibility ENDMETHOD.
sections METHOD display.
ENDMETHOD.
ENDCLASS.
© SAP AG 1999
- 25. 接口中只有定义。要完成的功能在 class 的
implementation 部分实现。接口中的部
件自动在类中可用。
接口不分为 public , private 等部分。
属性、方法、常量、类型等可以和类中的
一样定义
接口需要在类定义的 public 部分列出。
用法:
<interface name>~<component name>
- 26. 可以使用 TYPE TABLE OF REF TO
<interface> 关键字定义一个内表,来存
放属于不同类的对象。这些类必须都包含
这个接口。
- 27. Events
Events 类似一个类,有自己的属性和方
法,能被其它某一个类调用。
Event 只能有 EXPORTING 参数
要使用 Events ,需要以下几点。
在 class 中定义 events ,和它的触发条件
( events , raise )
定义 events ,以及它实现的功能( method xxx for
event of <class name> )
使用包含 events 的 object 前,对 event 对象使用
create object ,并且用 set handler 语句,使 event
能够被响应。
- 28. 三 . 使用 Control (控件)
在对屏幕的编程中,常常使用 Control 技
术,能够实现如 VC , Java 等语言的一些
功能,如 Event , Attribute 等。方便用
户和系统交互。
常见的 Control :
TextEdit , ALV , Picture , HTML
viewer 等
- 29. 做一个 RichText 控件的例子
1. 创建控件
使用 Control 前,我们要先在屏幕上创建
一个 Container 控件。 Control 在这个控
件中显示、处理。
- 31. 创建完 container 后,就可以在程序中,
调用系统自带的 class 了。这里需要用到两
个 class:
cl_gui_custom_container
cl_gui_textedit
创建 object 的时候,传入 container 的名
字做参数,并传入是否自动换行等参数
(这些参数,是传到 constructor 方法的
)
- 32. 效果如图,可以看到是一个空白的控件,并自带
了一些按钮。控件的大小和位置与 container 一
致。
- 33. 2. 使用 cl_gui_textedit 的其它功能
可以调用该类中的方法,如 SET_FOCUS ,
GET_VISIBLE 等,来完成其它面向语言中
的一些常见功能。
通过类中的 event ,可以实现和用户的交
互。可以自己定义 handler ,来响应用户
的 events 。
- 34. 由于操作 control 的方法都是通过 RFC 函
数传到 front-end 实现的,因此要使用
FLUSH 方法来保持 front-end 和后台的同
步。
写法: CALL METHOD cl_gui_cfw=>flush.