W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
術(shù)語多態(tài)性字面意思是“多種形式”。從面向?qū)ο蟮慕嵌葋砜?,多態(tài)性與繼承一起工作,使得繼承樹中的各種類型可以互換使用。也就是說,當(dāng)存在類的層次結(jié)構(gòu)并且它們通過繼承相關(guān)時,發(fā)生多態(tài)性。 ABAP多態(tài)意味著對方法的調(diào)用將導(dǎo)致根據(jù)調(diào)用方法的對象的類型執(zhí)行不同的方法。
以下程序包含一個抽象類'class_prgm',2個子類(class_procedural和class_OO)和一個測試驅(qū)動程序類'class_type_approach'。在這個實現(xiàn)中,類方法“開始”允許我們顯示編程的類型及其方法。如果仔細(xì)觀察方法“start”的簽名,您將觀察到它接收到類型為class_prgm的導(dǎo)入?yún)?shù)。但是,在Start-Of-Selection事件中,此方法在運(yùn)行時已調(diào)用類型為class_procedural和class_OO的對象。
Report ZPolymorphism1. CLASS class_prgm Definition Abstract. PUBLIC Section. Methods: prgm_type Abstract, approach1 Abstract. ENDCLASS. CLASS class_procedural Definition Inheriting From class_prgm. PUBLIC Section. Methods: prgm_type Redefinition, approach1 Redefinition. ENDCLASS. CLASS class_procedural Implementation. Method prgm_type. Write: 'Procedural programming'. EndMethod. Method approach1. Write: 'top-down approach'. EndMethod. ENDCLASS. CLASS class_OO Definition Inheriting From class_prgm. PUBLIC Section. Methods: prgm_type Redefinition, approach1 Redefinition. ENDCLASS. CLASS class_OO Implementation. Method prgm_type. Write: 'Object oriented programming'. EndMethod. Method approach1. Write: 'bottom-up approach'. EndMethod. ENDCLASS. CLASS class_type_approach Definition. PUBLIC Section. CLASS-METHODS: start Importing class1_prgm Type Ref To class_prgm. ENDCLASS. CLASS class_type_approach IMPLEMENTATION. Method start. CALL Method class1_prgm→prgm_type. Write: 'follows'. CALL Method class1_prgm→approach1. EndMethod. ENDCLASS. Start-Of-Selection. Data: class_1 Type Ref To class_procedural, class_2 Type Ref To class_OO. Create Object class_1. Create Object class_2. CALL Method class_type_approach?start Exporting class1_prgm = class_1. New-Line. CALL Method class_type_approach?start Exporting class1_prgm = class_2.
上面的代碼產(chǎn)生以下輸出:
Procedural programming follows top-down approach Object oriented programming follows bottom-up approach
ABAP運(yùn)行時環(huán)境在導(dǎo)入?yún)?shù)class1_prgm的分配期間執(zhí)行隱式精簡轉(zhuǎn)換。此功能有助于“啟動”方法的一般實現(xiàn)。與對象引用變量相關(guān)聯(lián)的動態(tài)類型信息允許ABAP運(yùn)行時環(huán)境動態(tài)地將方法調(diào)用與由對象引用變量指向的對象中定義的實現(xiàn)綁定。例如,'class_type_approach'類中的'start'方法的導(dǎo)入?yún)?shù)'class1_prgm'指的是一個永遠(yuǎn)不能被實例化的抽象類型。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: