*====================================================
* 定义实现类与对象 方法一
*====================================================
*-------------------------------------------------
* ------CLASS ONE DEFINITION------
*-------------------------------------------------
CLASS one DEFINITION. "定义一个类
PUBLIC SECTION .
"数据准备
TYPES: ty_age TYPE i.
DATA: name TYPE string,
role TYPE string,
home TYPE string,
age TYPE ty_age.
METHODS:
person_info. "在类中定义一个方法
ENDCLASS.
*------------------------------------------------
* -----------CLASS ONE IMPLEMENTATION---------
*------------------------------------------------
"创建类的实施
CLASS one IMPLEMENTATION.
METHOD person_info. "创建类中方法的具体实现
WRITE: /,
/,'实验数据'.
WRITE: / '名字:',name.
WRITE: / '角色:',role.
WRITE: / '家庭:',home.
WRITE: / '年龄:',age.
ENDMETHOD.
ENDCLASS.
"开始测试调用已定义的类和方法
START-OF-SELECTION.
"参考类创建变量
DATA: person1 TYPE REF TO one,
person2 TYPE REF TO one.
"创建类ONE的实例person1
CREATE OBJECT person1.
person1->name = '实验体1号'. "赋予值
person1->role = '实验品'. "赋予值
person1->home = '储物箱1号'. "赋予值
person1->age = '1'. "赋予值
"创建类ONE的实例person2
CREATE OBJECT person2.
person2->name = '实验体2号'. "赋予值
person2->role = '实验品'. "赋予值
person2->home = '储物箱2号'. "赋予值
person2->age = '2'.
"调用方法
person1->person_info( ).
person2->person_info( ).
.
方法二
效果展示
代码展示
*====================================================
* 定义实现类与对象 方法二
*====================================================
*-----------------------------------------------------
* -------CLASS TWO DEFINITION--------
*-----------------------------------------------------
CLASS two DEFINITION.
PUBLIC SECTION. "Public 在前, Private在后
"数据准备
TYPES: ty_value TYPE i.
"构造方法
METHODS: "'constructor'为固定值不可修改
constructor IMPORTING country TYPE string
name TYPE string
type TYPE string
value TYPE ty_value,
set_value IMPORTING new_value TYPE ty_value,
treasure_info.
PRIVATE SECTION.
"定义私人部分变量
DATA: country TYPE string,
name TYPE string,
type TYPE string,
value TYPE ty_value.
ENDCLASS.
*-------------------------------------------------
* -----------CLASS TWO IMPLEMENTATION---------
*-------------------------------------------------
CLASS two IMPLEMENTATION.
"方法的实现
METHOD constructor. "初始赋值方法
me->country = country.
me->name = name.
me->type = type.
me->value = value.
ENDMETHOD.
METHOD set_value. "修改新值方法
me->value = new_value.
ENDMETHOD.
METHOD treasure_info. "信息输出方法
WRITE: /,
/,'宝物数据'.
WRITE: / '国家:',country.
WRITE: / '名称:',name.
WRITE: / '类型:',type.
WRITE: / '价值:',value.
ENDMETHOD.
ENDCLASS.
"开始测试
START-OF-SELECTION.
DATA:treasure1 TYPE REF TO two,
treasure2 TYPE REF TO two.
CREATE OBJECT treasure1
EXPORTING
country = '日本'
name = 'Saber的大宝剑'
type = '金色传说'
value = '9999'.
CREATE OBJECT treasure2
EXPORTING
country = '中国'
name = '百分百空手接白刃秘籍'
type = '哪吒专属'
value = '888'.
treasure1->treasure_info( ).
treasure2->treasure_info( ).
treasure2->set_value( '666' ).
treasure2->treasure_info( ).