1, 抽象类和抽象方法(Abstract)
抽象类和抽象方法.
一个基类可能包含多个派生类, 但该基类只是作为模板出现的, 并不需要有任何对象作为实例, 则可以将该类声明为抽象类(Abstract Class).
1 2 3 |
CLASS class1 DEFINITION ABSTRACT. ... ENDCLASS. |
抽象类不能使用 CREATE OBJECT 语句创建类对象, 其主要目的是为了做派生类的模板, 实际使用的对象则依照各个派生类创建.
也可以将方法定义为抽象方法.
1 |
METHODS meth ABSTRACT. |
一个抽象方法不能在类本身中实现, 而必须在其派生出的非抽象类中实现. 因而含有抽象方法的类必须定义为抽象类.
2, 最终类和最终方法(Final)
最终类和最终方法.
最终类(Final Class), 是不能被继承的类, 其主要意义在于: 设计过程中, 如果一个类有很多级别的派生类, 则其中公有和被保护成员方法, 随意改动该类, 很容易造成派生类中的语法和主义冲突. 因而, 如果不希望在程序中继续对该类进行派生, 则可以定义该类为最终类.
1 2 3 |
CLASS class1 DEFINITION FINAL. ... ENDCLASS. |
最终方法是不可以重新定义的方法, 不一定出现在最终类中, 但最终类中的所有方法都是最终方法.
1 |
METHODS meth ABSTRACT ... |
3, 例子程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
REPORT ztest_oo_abstract. *&---------------------------------------------------------------------* *& Class cl_superclass *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS cl_superclass DEFINITION ABSTRACT. PUBLIC SECTION. DATA:para(30) TYPE c VALUE 'The super abstract method'. METHODS write_first ABSTRACT. ENDCLASS. "cl_superclass *&---------------------------------------------------------------------* *& Class cl_subclass *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS cl_subclass DEFINITION INHERITING FROM cl_superclass. PUBLIC SECTION. METHODS write_first REDEFINITION. METHODS write_finalmethod FINAL. ENDCLASS. "cl_subclass *&---------------------------------------------------------------------* *& Class cl_finalclass *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS cl_finalclass DEFINITION FINAL. PUBLIC SECTION. METHODS:write_cl_finalclass. ENDCLASS. "cl_finalclass *&---------------------------------------------------------------------* *& Class (Implementation) cl_subclass *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS cl_subclass IMPLEMENTATION . METHOD write_first. WRITE:para. ENDMETHOD. "write_first METHOD write_finalmethod . WRITE: / 'The final method'. ENDMETHOD. "write_sub ENDCLASS. "cl_subclass *&---------------------------------------------------------------------* *& Class (Implementation) cl_finalclass *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS cl_finalclass IMPLEMENTATION. METHOD write_cl_finalclass. WRITE: / 'The final class'. ENDMETHOD. "write_cl_finalclass ENDCLASS. "cl_finalclass DATA go_inher_obj TYPE REF TO cl_subclass. DATA go_final_obj TYPE REF TO cl_finalclass. START-OF-SELECTION. CREATE OBJECT go_inher_obj. CREATE OBJECT go_final_obj. CALL METHOD go_inher_obj->write_first. CALL METHOD go_inher_obj->write_finalmethod. CALL METHOD go_final_obj->write_cl_finalclass. |
运行结果:
以上。
发表评论