[OO]ABAP OO 文法--執行個體看 "="與"?=" 的區別[轉]

來源:互聯網
上載者:User

標籤:

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.                 "destructorENDCLASS.                    "ac IMPLEMENTATIONSTART-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.                 "destructorENDCLASS.                    "ac IMPLEMENTATIONSTART-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 文法--執行個體看 "="與"?=" 的區別[轉]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.