ABAP 类

举报
雨绸缪 发表于 2023/07/31 17:12:31 2023/07/31
【摘要】 类是我们需要理解和掌握的基本结构,以便为实体建模。一个类可以代表任何东西;一个文件、一辆车、动物,或者任何有特征和行为的东西。ABAP 中的类的编码分为两步:首先,你编码定义部分,然后是实现部分。定义部分是你定义所有数据和方法的地方,这些方法将在类中使用。在这里,你必须指定 public、private 和 protected 部分,而且它们必须按照你在代码中看到的顺序放置,否则你会出现语法...

类是我们需要理解和掌握的基本结构,以便为实体建模。

一个类可以代表任何东西;一个文件、一辆车、动物,或者任何有特征和行为的东西。

ABAP 中的类的编码分为两步:首先,你编码定义部分,然后是实现部分。定义部分是你定义所有数据和方法的地方,这些方法将在类中使用。

在这里,你必须指定 public、private 和 protected 部分,而且它们必须按照你在代码中看到的顺序放置,否则你会出现语法错误。

下面的代码显示了一个类的结构:

CLASS <Name of the class> DEFINITION.
  PUBLIC SECTION.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS <Name of the class> IMPLEMENTATION.
ENDCLASS.

请创建一个新程序,命名为 ZCLASS 并写入以下代码:

report  zclass.

class cl_animal definition abstract.
  public section.
    methods: constructor importing i_name type string, " Visible to everyone
             make_a_sound,
             my_name_is,
             get_type,
             introduce_me.

  protected section. " Visible only in child classes
    data p_class type string.

  private section. " Visible only internally
    data p_name type string.
endclass.

class cl_animal implementation.
  method constructor.
    p_name = i_name.     " p_name was defined already in the definition part of the class as private
    p_class = 'Unknown'. " p_class was defined already in the definition part of the class as protected
  endmethod.

  method make_a_sound.
    write 'Nothing'.
  endmethod.

  method my_name_is.
    write: / 'My name is: ', p_name.
  endmethod.

  method get_type.
    write: / 'I''m type of: ', p_class.
  endmethod.

  method introduce_me.
    me->my_name_is( ). " The keyword 'me' is used to specify class member. Is the equivalent of the keyword 'this' in C#
    make_a_sound( ).
    get_type( ).
  endmethod.
endclass.

class cl_dog definition inheriting from cl_animal.
  public section.
    methods: constructor importing i_dog_name type string,
             make_a_sound redefinition. " Change the behaviour of the method. Reimplement the code.
endclass.

class cl_dog implementation.
  method constructor.
    super->constructor( i_dog_name ). " Initialize the constructor and internally pass the parameter to the abstract class
    p_class = '"Dog"'.                " This is the protected member which is visible only in child classes
  endmethod.

  method make_a_sound.
    write: / 'My sound is:', 'Woof, woof'.
  endmethod.
endclass.

class cl_cat definition inheriting from cl_animal.
  public section.
    methods: constructor importing i_cat_name type string,
             make_a_sound redefinition.
endclass.

class cl_cat implementation.
  method constructor.
    super->constructor( i_cat_name ).
    p_class = '"Cat"'.
  endmethod.

  method make_a_sound.
    write: / 'My sound is:', 'Meow, meow'.
  endmethod.
endclass.

class cl_animal_factory definition.
  public section.
    class-methods create_animal importing i_animal type i returning value(r_animal) type ref to cl_animal. " Class method, in C# this is called a static method
endclass.

class cl_animal_factory implementation. " Factory pattern
  method create_animal.
    case i_animal.
      when 1.
        data dog type ref to cl_dog.
        create object dog exporting i_dog_name = 'Sparky'.
        r_animal = dog. " It is returned a cl_dog instance.
      when 2.
        data cat type ref to cl_cat.
        create object cat exporting i_cat_name = 'Fluffy'.
        r_animal = cat. " It is returned a cl_cat instance.
      when others.
    endcase.
  endmethod.
endclass.

class cl_introducer definition.
  public section.
    class-methods introduce importing i_animal type ref to cl_animal. " Here the method receives a cl_animal type parameter
endclass.

class cl_introducer implementation.
  method introduce.
    if i_animal is not initial.
      i_animal->introduce_me( ).
    else.
      write / 'I''m nothing'.
    endif.
  endmethod.
endclass.


start-of-selection.
  data wa_animal type ref to cl_animal.

  wa_animal = cl_animal_factory=>create_animal( 1 ).
  cl_introducer=>introduce( wa_animal ). " The i_animal parameter is implicitly specified. Useful when is only one parameter.
  write /.

  wa_animal = cl_animal_factory=>create_animal( 2 ).
  cl_introducer=>introduce( i_animal = wa_animal ). "  The i_animal parameter is explicitly specified and is necessary its use when is more than one paramter.
  write /.

  wa_animal = cl_animal_factory=>create_animal( 3 ).
  cl_introducer=>introduce( wa_animal ).

根据定义,类可以被创建为普通的、抽象的和继承的。

  • 公共部分是你声明在类外可见信息的地方。

  • 私有部分有内部成员,即使在子类中也不能暴露在类外。

  • 受保护的部分有只在内部公开或可见的信息,并与所有子类一起。

在每个部分,你都可以指定实例数据和方法,也可以指定类的数据和方法。

实例成员和类成员的区别在于,对于前者来说,必须创建对象(我们很快就会知道)才能使用这些成员,而对于后者来说,不需要实例化,我们可以直接从类中使用它们,并使用一个特殊的符号( =>),对于其他实例数据和方法,则使用(->)符号。

class cl_animal definition abstract.
  public section.
    methods: constructor importing i_name type string, " Visible to everyone
             make_a_sound,
             my_name_is,
             get_type.

  protected section. " Visible only here and in child classes
    data p_class type string.

  private section. " Visible only internally
    data p_name type string.
endclass.

实现部分是用来编码方法的。在这里你对类的行为进行编码,你可以看到包括参数在内的所有成员都没有出现,这是因为它们已经在类的定义部分被定义了。

class cl_animal implementation.
  method constructor.
    p_name = i_name.     " p_name was defined already in the definition part of the class as private
    p_class = 'Unknown'. " p_class was defined already in the definition part of the class as protected
  endmethod.

  method make_a_sound.
    write 'Nothing'.
  endmethod.

  method my_name_is.
    write: / 'My name is: ', p_name.
  endmethod.

  method get_type.
    write: / 'I''m type of: ', p_class.
  endmethod.
endclass.
推荐

华为开发者空间发布

让每位开发者拥有一台云主机

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。