首页 > 代码库 > [OO]ABAP OO 语法--实例看 "="与"?=" 的区别[转]

[OO]ABAP OO 语法--实例看 "="与"?=" 的区别[转]

ABAP OO 面向对象语法
   实例看  "=" 与 "?=" 的区别
  
                
1. "="  同类型赋值
*&---------------------------------------------------------------------*
*& Report  Z_WYS_TYPEREF01
*&作者:永上
*&---------------------------------------------------------------------*
*&同类之间用 = 赋值
*&
*&---------------------------------------------------------------------*
REPORT  z_wys_typeref01.
 
*----------------------------------------------------------------------*
*       CLASS CP DEFINITION
*----------------------------------------------------------------------*
*c_parent 定义
*----------------------------------------------------------------------*
CLASS c_parent DEFINITION ." 父类
  PUBLIC SECTION.
    DATA i TYPE i.
    DATA s TYPE string.
    METHODS constructor.
    METHODS zwrite.
  PRIVATE SECTION.

ENDCLASS.                    "A DEFINITION

*----------------------------------------------------------------------*
*       CLASS c_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*c_parent 实现
*----------------------------------------------------------------------*
CLASS c_parent IMPLEMENTATION.
  METHOD constructor.
    WRITE / ‘父类创建!‘.
  ENDMETHOD.                    "constructor
  METHOD zwrite   .
    WRITE / ‘父类write.‘.
  ENDMETHOD.                 "destructor
  "destructor
ENDCLASS .               "a
 
START-OF-SELECTION.
  DATA cp  TYPE REF TO c_parent . "
  DATA cp2 TYPE REF TO c_parent . "
  CREATE OBJECT cp.   "cp 实例化, 系统会在后台创建一个
                      "c_parent类型的实例(比如命名为c_parent_1)
                      "并且让cp 引用这个实例

  cp->zwrite( ).      "调用成员函数
  cp2 = cp.           " 静态声明 cp 和 cp2是相同类型.
                      " cp2 并没有实例化(后台没有创建c_parent_2),
                      " cp2只是引用了cp引用的实例.
  cp->i = 10.         " cp2->i也随之改为10. 这就是引用类型的魅力^_^
  write: / cp->i,cp2->i.
  cp2->i = 99.         " cp->i也随之改为99. 这也是引用类型的魅力^_^
  write: / cp->i,cp2->i.
  cp2->zwrite( ).     "调用成员函数

2. "="  向上转型
*&---------------------------------------------------------------------*
*& Report  Z_WYS_TYPEREF
*&
*&---------------------------------------------------------------------*
*&
*&  基类 = 派生类
*&---------------------------------------------------------------------*
REPORT  z_wys_typeref02.
 
*----------------------------------------------------------------------*
*       CLASS CP DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent DEFINITION ." 基类
  PUBLIC SECTION.
    DATA i TYPE i.
    DATA s TYPE string.
    METHODS constructor.
    METHODS zwrite.
  PRIVATE SECTION.

ENDCLASS.                    "A DEFINITION

*----------------------------------------------------------------------*
*       CLASS c_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent IMPLEMENTATION.
  METHOD constructor.
     " 父类创建
    WRITE / ‘父类创建!‘.
  ENDMETHOD.
    METHOD zwrite   .
    WRITE / ‘父类write.‘.
  ENDMETHOD.                 "destructor
                    "destructor
ENDCLASS .               "a

*----------------------------------------------------------------------*
*       CLASS c_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child DEFINITION INHERITING FROM c_parent.
  PUBLIC SECTION.
    DATA child_str TYPE string.
    METHODS constructor .
    METHODS zwrite REDEFINITION.
ENDCLASS.                    "
*----------------------------------------------------------------------*
*       CLASS ac IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child IMPLEMENTATION.
  METHOD constructor .  " 子类创建
    super->constructor( )."先调用父类的构造函数
    WRITE  ‘子类创建!‘.
  ENDMETHOD.
  METHOD zwrite   .
    WRITE / ‘子类write.‘.
  ENDMETHOD.                 "destructor
ENDCLASS.                    "ac IMPLEMENTATION
START-OF-SELECTION.
  DATA cp  TYPE REF TO c_parent . "
  DATA cc  TYPE REF TO c_child . "
  CREATE OBJECT cc ." 创建子类实例
  cp = cc.          " 父类 = 子类
                    " 静态声明中"=" 两边类型不匹配,
                    " 但是"=" 左边是右边的基类
                    " 所以语法检查通过
  cp->zwrite( ).    " 调用子类的方法
3. "?=" 向下转型
*&---------------------------------------------------------------------*
*& Report  Z_WYS_TYPEREF
*&
*&---------------------------------------------------------------------*
*&
*&  派生类 = 基类
*&---------------------------------------------------------------------*
REPORT  z_wys_typeref03.
 
*----------------------------------------------------------------------*
*       CLASS CP DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent DEFINITION ." 父类
  PUBLIC SECTION.
    DATA i TYPE i.
    DATA s TYPE string.
    METHODS constructor.
    METHODS zwrite.
  PRIVATE SECTION.

ENDCLASS.                    "A DEFINITION

*----------------------------------------------------------------------*
*       CLASS c_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent IMPLEMENTATION.
  METHOD constructor.
     " 父类创建
    WRITE / ‘父类创建!‘.
  ENDMETHOD.
    METHOD zwrite   .
    WRITE / ‘父类write.‘.
  ENDMETHOD.                 "destructor
                    "destructor
ENDCLASS .               "a

*----------------------------------------------------------------------*
*       CLASS c_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child DEFINITION INHERITING FROM c_parent.
  PUBLIC SECTION.
    DATA child_str TYPE string.
    METHODS constructor .
    METHODS zwrite REDEFINITION.
ENDCLASS.                    "
*----------------------------------------------------------------------*
*       CLASS ac IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child IMPLEMENTATION.
  METHOD constructor .  " 子类创建
    super->constructor( )."先调用父类的构造函数
    WRITE  ‘子类创建!‘.
  ENDMETHOD.
  METHOD zwrite   .
    WRITE / ‘子类write.‘.
  ENDMETHOD.                 "destructor
ENDCLASS.                    "ac IMPLEMENTATION
START-OF-SELECTION.
  DATA cp  TYPE REF TO c_parent . "
  DATA cc  TYPE REF TO c_child . "
*******************************************************
* 错误1
"  CREATE OBJECT cp ." 创建父类实例
"  cc = cp.         " 子类 = 父类 语法检查出错
                    " 静态声明中"=" 两边类型不匹配.
*******************************************************
*******************************************************
* 错误2
"  CREATE OBJECT cp ." 创建父类实例
"  cc ?= cp.         " 语法检查正确, 但 运行期类型不匹配
*******************************************************
*正确
  CREATE OBJECT cp type c_child." 动态创建cp 为c_child
  cc ?= cp.         " 静态语法检查正确, 动态类型一致
  cc->zwrite( ).    " 调用子类的方法
 
 
  举例完毕.
  关于 "?=", SAP 帮助中这么解释的:
 
  MOVE source {TO|?TO} destination.
  destination {=|?=} source.
  原文:
  Both these statements assign the content of the operand source to
  the data object destination. The variants with the addition TO or
  the assignment operator = are valid for all assignments between
  operands that are not reference variables,
  and for assignments between reference variables for which the
  static type of source is more specific than or the same as the
  static type of destination (narrowing cast).
  中文:
  上述两个声明,用源操作数的数据(source)给目标数据对象赋值(destination).
  "TO"或者赋值运算符"=" 适用于以下两种情况:
  1. 操作对象,包括源和目标对象都不是引用类型
  2. 如果是引用类型,源对象和目标对象类型一致(本文例子1)或者源对象要比目标对象更具体(向上转型)--(本文例子2)
 
 
  原文:
  Variants with the addition ?TO or the assignment operator ?= (casting operator)
  must be used if the source and destination are reference variables
   and the static type of source is more general than the static type
   of destination (down cast).
  For assignments between operands that are not reference variables,
  use of the question mark ? is not permitted.
  中文:
  使用"?TO"或者赋值运算符"?="(类型转换操作符)的变式,要同时具备以下两个条件:
  1.操作对象,包括源和目标对象, 都是引用类型;
  2.并且,源对象的静态类型(data语句声明的类型)要比目标对象更抽象(向下转型).--(本文例子3)
  非引用型变量赋值不能用问号操作符.

[OO]ABAP OO 语法--实例看 "="与"?=" 的区别[转]