SlideShare ist ein Scribd-Unternehmen logo
1 von 34
ABAP 面向对象编程

一.   介绍
二.   主要概念
三.   使用 Control
四.   ALV Control 例子
一 . 介绍
   传统的 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
面向对象编程
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
ABAP Objects 是 ABAP 的扩展,集合了 Java , C ++,
Smalltalk 等语言的特点。和原来的 ABAP 无缝集成。
  History of Programming Languages


                          Machine language
                             Assembler




                                                 ABAP
                                  C++




                                  Java

                                             ABAP Objects




    © SAP AG 1999
二 . 主要概念
   Class

   在 SAP 中,可以在 SE24 或 ABAP 编辑器中定义
    Class 。二者的区别类似 SE11 中定义的结构与
    程序中定义的结构。
   在 SE24 中,可以看到类的各个部件,条理清楚。
    代码中的关键字,如 public , class-method 等
    在 SE24 中都可以体现出来。
在 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
定义 Class 的一个例子
Objects
   Object :就是 class 的实例
    ( instance )
   使用 object 前,先要用下面的语句来定义
    该 object 属于哪个类
    data <object name> type ref to <class name>
   定义完该对象后,就可以使用 create object
    <object name> 来创建对象了。这时系统会
    分配一块内存区域给对象。
    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
    如果内存中某个 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
   可以使用 TYPE TABLE OF REF TO <class> 关键字定义一个内表,
    来存放该类的多个对象。
Attribute
   可以是各种类型,包括:

   Data types: scalar ( 例如 data element),
    structured, in tables ,
   ABAP elementary types (C, I, ...)
   Object references
   Interface references
   按定义位置,分为 Public 和 Private.

   Public Attribute :能从 class 外直接访问和修
    改。一般来说,尽量少定义 Public
    Attribute 。

   Private Attribute :只能从类中访问和修改。
   按定义的关键字,分为 Instance 和 Static.
   Instance Attribute :同一个类中多个实例的 Instance
    Attribute 各自分开,具有不同的值。使用 DATA 关键字
    定义。
    用法:实例名 -> 属性

Static Attribute :同一个类中多个实例的 Static
 Attribute 的值一样。可以通过各个实例或直接通过类来
 访问和修改。使用 CLASS-DATA 定义。在 debug 时可
 以看到 class-data 不占用 object 的内存。
用法:实例名 -> 属性
      或 类名 => 属性
Method
   按定义位置,分为 Public 和 Private. 类
    似 Attribute 。

   Public Method : 能从类外调用。

   Private Method :只能从该类中调用。
   按定义的关键字,分为 Instance 和
    Static.

   Instance Method :能使用 static 和 instance 的内容。
    使用 Method 定义。
   用法:实例名 -> 方法名

   Static Method :只能使用 static 的内容。可以不通过实
    例,直接调用。使用 Class-Method 定义。
   用法:实例名 -> 方法名
        或 类名 => 方法名
Method 的参数
   Method 的参数可以是 Class/Object
   可以有 importing, exporting,
    changing, returning 等
Constructor 方法(初始化)
   每个类都自动隐性带有名为 constructor
    ( Instance )和
    class_constructor ( Static )方法。

   要使用这两个 method ,必需首先定义它
    们。 constructor 可以有参数和例外,而
    class_constructor 没有参数。
   每次使用 create object 时自动执行
    constructor 方法 .

   Class-constructor 在一个程序中只执行
    一次。在以下事件前自动执行。
   CREATE OBJECT
   调用该类的属性
   使用 Call Method
   Registering a static event handler
   Registering an evetm handler method for a static
    event
   如果 constructor 带有参数,在 create
    object 时就能够通过传入的参数初始化
    object 。
     例如
CREATE OBJECT airplane1 exporting
  p2_name = 'Hansemand'
  p2_planetype = 'Boing 747'.
Protected
   protected sector 中的内容用可以被该类
    自己,其父类和子类访问。
继承
   可以有多级继承
   一个子类只有一个直接父类
   类知道自己的 superclass ,但不知道自己
    的 subclass
   继承时, superclass 的内容不能除去,只
    能加入新的内容
   用法:定义 class 时,使用关键字
    INHERITING FROM
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
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
   接口中只有定义。要完成的功能在 class 的
    implementation 部分实现。接口中的部
    件自动在类中可用。
   接口不分为 public , private 等部分。
   属性、方法、常量、类型等可以和类中的
    一样定义
   接口需要在类定义的 public 部分列出。
   用法:
<interface name>~<component name>
   可以使用 TYPE TABLE OF REF TO
    <interface> 关键字定义一个内表,来存
    放属于不同类的对象。这些类必须都包含
    这个接口。
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
    能够被响应。
三 . 使用 Control (控件)
   在对屏幕的编程中,常常使用 Control 技
    术,能够实现如 VC , Java 等语言的一些
    功能,如 Event , Attribute 等。方便用
    户和系统交互。
   常见的 Control :
    TextEdit , ALV , Picture , HTML
    viewer 等
做一个 RichText 控件的例子
             1. 创建控件
   使用 Control 前,我们要先在屏幕上创建
    一个 Container 控件。 Control 在这个控
    件中显示、处理。
   创建完 container 后,就可以在程序中,
    调用系统自带的 class 了。这里需要用到两
    个 class:
    cl_gui_custom_container
    cl_gui_textedit
   创建 object 的时候,传入 container 的名
    字做参数,并传入是否自动换行等参数
    (这些参数,是传到 constructor 方法的
    )
   效果如图,可以看到是一个空白的控件,并自带
    了一些按钮。控件的大小和位置与 container 一
    致。
2. 使用 cl_gui_textedit 的其它功能
   可以调用该类中的方法,如 SET_FOCUS ,
    GET_VISIBLE 等,来完成其它面向语言中
    的一些常见功能。
   通过类中的 event ,可以实现和用户的交
    互。可以自己定义 handler ,来响应用户
    的 events 。
   由于操作 control 的方法都是通过 RFC 函
    数传到 front-end 实现的,因此要使用
    FLUSH 方法来保持 front-end 和后台的同
    步。
    写法: CALL METHOD cl_gui_cfw=>flush.

Weitere ähnliche Inhalte

Was ist angesagt?

Hibernate 映射配置文件详解
Hibernate 映射配置文件详解Hibernate 映射配置文件详解
Hibernate 映射配置文件详解wpscbbn405
 
千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7javatwo2011
 
Java相关基础知识
Java相关基础知识Java相关基础知识
Java相关基础知识yiditushe
 
Java程序员面试之葵花宝典
Java程序员面试之葵花宝典Java程序员面试之葵花宝典
Java程序员面试之葵花宝典yiditushe
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Java Script 引擎技术
Java Script 引擎技术Java Script 引擎技术
Java Script 引擎技术bigqiang zou
 
2, object oriented programming
2, object oriented programming2, object oriented programming
2, object oriented programmingted-xu
 
Java SE 7 技術手冊第六章草稿 - 何謂繼承?
Java SE 7 技術手冊第六章草稿 - 何謂繼承?Java SE 7 技術手冊第六章草稿 - 何謂繼承?
Java SE 7 技術手冊第六章草稿 - 何謂繼承?Justin Lin
 
如何在 Java App 中導入 Scala
如何在 Java App 中導入 Scala如何在 Java App 中導入 Scala
如何在 Java App 中導入 Scalajavatwo2011
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-CRyan Chung
 
2. java introduction
2. java introduction2. java introduction
2. java introductionnetdbncku
 
《Python 3.5 技術手冊》第六章草稿
《Python 3.5 技術手冊》第六章草稿《Python 3.5 技術手冊》第六章草稿
《Python 3.5 技術手冊》第六章草稿Justin Lin
 
Oracle公司内部数据库培训资料
Oracle公司内部数据库培训资料Oracle公司内部数据库培训资料
Oracle公司内部数据库培训资料yiditushe
 
[圣思园][Java SE]Reflection
[圣思园][Java SE]Reflection[圣思园][Java SE]Reflection
[圣思园][Java SE]ReflectionArBing Xie
 

Was ist angesagt? (20)

Hibernate 映射配置文件详解
Hibernate 映射配置文件详解Hibernate 映射配置文件详解
Hibernate 映射配置文件详解
 
千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7
 
Java相关基础知识
Java相关基础知识Java相关基础知识
Java相关基础知识
 
Java程序员面试之葵花宝典
Java程序员面试之葵花宝典Java程序员面试之葵花宝典
Java程序员面试之葵花宝典
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Java Script 引擎技术
Java Script 引擎技术Java Script 引擎技术
Java Script 引擎技术
 
Java 網路程式
Java 網路程式Java 網路程式
Java 網路程式
 
Dev307
Dev307Dev307
Dev307
 
2, object oriented programming
2, object oriented programming2, object oriented programming
2, object oriented programming
 
Java SE 7 技術手冊第六章草稿 - 何謂繼承?
Java SE 7 技術手冊第六章草稿 - 何謂繼承?Java SE 7 技術手冊第六章草稿 - 何謂繼承?
Java SE 7 技術手冊第六章草稿 - 何謂繼承?
 
如何在 Java App 中導入 Scala
如何在 Java App 中導入 Scala如何在 Java App 中導入 Scala
如何在 Java App 中導入 Scala
 
SCJP ch17
SCJP ch17SCJP ch17
SCJP ch17
 
Java annotation
Java annotationJava annotation
Java annotation
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-C
 
Scala
ScalaScala
Scala
 
2. java introduction
2. java introduction2. java introduction
2. java introduction
 
《Python 3.5 技術手冊》第六章草稿
《Python 3.5 技術手冊》第六章草稿《Python 3.5 技術手冊》第六章草稿
《Python 3.5 技術手冊》第六章草稿
 
Oracle公司内部数据库培训资料
Oracle公司内部数据库培训资料Oracle公司内部数据库培训资料
Oracle公司内部数据库培训资料
 
beidakejian
beidakejianbeidakejian
beidakejian
 
[圣思园][Java SE]Reflection
[圣思园][Java SE]Reflection[圣思园][Java SE]Reflection
[圣思园][Java SE]Reflection
 

Andere mochten auch

Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5prakash185645
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programsKranthi Kumar
 
Smartforms interview questions with answers
Smartforms interview questions with answersSmartforms interview questions with answers
Smartforms interview questions with answersUttam Agrawal
 
SAP ABAP Latest Interview Questions with Answers by Garuda Trainings
SAP ABAP Latest Interview Questions with Answers by Garuda TrainingsSAP ABAP Latest Interview Questions with Answers by Garuda Trainings
SAP ABAP Latest Interview Questions with Answers by Garuda TrainingsGaruda Trainings
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Andere mochten auch (8)

Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programs
 
Smartforms interview questions with answers
Smartforms interview questions with answersSmartforms interview questions with answers
Smartforms interview questions with answers
 
Badi document
Badi documentBadi document
Badi document
 
Abap reports
Abap reportsAbap reports
Abap reports
 
SAP ABAP Latest Interview Questions with Answers by Garuda Trainings
SAP ABAP Latest Interview Questions with Answers by Garuda TrainingsSAP ABAP Latest Interview Questions with Answers by Garuda Trainings
SAP ABAP Latest Interview Questions with Answers by Garuda Trainings
 
SAP ABAP Material
SAP ABAP MaterialSAP ABAP Material
SAP ABAP Material
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Ähnlich wie Abap oo

Ruby的类和对象模型
Ruby的类和对象模型Ruby的类和对象模型
Ruby的类和对象模型yinhm .
 
GDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinGDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinFCUGDSC
 
深入理解Andorid重难点
深入理解Andorid重难点深入理解Andorid重难点
深入理解Andorid重难点Bin Shao
 
Introduction to C++ over CLI
Introduction to C++ over CLIIntroduction to C++ over CLI
Introduction to C++ over CLI建興 王
 
lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫建興 王
 
物件導向程式設計課程講義(98 ges hi版)
物件導向程式設計課程講義(98 ges hi版)物件導向程式設計課程講義(98 ges hi版)
物件導向程式設計課程講義(98 ges hi版)Hui-Shih Leng
 
Python面向对象开发基础篇
Python面向对象开发基础篇Python面向对象开发基础篇
Python面向对象开发基础篇modou li
 
Java注解详解
Java注解详解Java注解详解
Java注解详解zlzl245437
 
[圣思园][Java SE]Java se lesson 3
[圣思园][Java SE]Java se lesson 3[圣思园][Java SE]Java se lesson 3
[圣思园][Java SE]Java se lesson 3ArBing Xie
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 
Class Inheritance
Class InheritanceClass Inheritance
Class Inheritancefinian lau
 

Ähnlich wie Abap oo (20)

Ruby的类和对象模型
Ruby的类和对象模型Ruby的类和对象模型
Ruby的类和对象模型
 
Hibernate
HibernateHibernate
Hibernate
 
hibernate
hibernatehibernate
hibernate
 
Js培训
Js培训Js培训
Js培训
 
Hibernate教程
Hibernate教程Hibernate教程
Hibernate教程
 
SCJP ch12
SCJP ch12SCJP ch12
SCJP ch12
 
GDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinGDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 Kotlin
 
深入理解Andorid重难点
深入理解Andorid重难点深入理解Andorid重难点
深入理解Andorid重难点
 
SCJP ch09
SCJP ch09SCJP ch09
SCJP ch09
 
Les3
Les3Les3
Les3
 
Introduction to C++ over CLI
Introduction to C++ over CLIIntroduction to C++ over CLI
Introduction to C++ over CLI
 
lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫
 
物件導向程式設計課程講義(98 ges hi版)
物件導向程式設計課程講義(98 ges hi版)物件導向程式設計課程講義(98 ges hi版)
物件導向程式設計課程講義(98 ges hi版)
 
Python面向对象开发基础篇
Python面向对象开发基础篇Python面向对象开发基础篇
Python面向对象开发基础篇
 
Java注解详解
Java注解详解Java注解详解
Java注解详解
 
[圣思园][Java SE]Java se lesson 3
[圣思园][Java SE]Java se lesson 3[圣思园][Java SE]Java se lesson 3
[圣思园][Java SE]Java se lesson 3
 
Les 3 ppt
Les 3 pptLes 3 ppt
Les 3 ppt
 
Metadata4shenzhen Final
Metadata4shenzhen FinalMetadata4shenzhen Final
Metadata4shenzhen Final
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 
Class Inheritance
Class InheritanceClass Inheritance
Class Inheritance
 

Abap oo

  • 1. ABAP 面向对象编程 一. 介绍 二. 主要概念 三. 使用 Control 四. ALV Control 例子
  • 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 在这个控 件中显示、处理。
  • 30.
  • 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.