ABAP object-oriented-use Constructor

Source: Internet
Author: User

This section defines class initialization through the example given in the previous section.

*----------------------------------------------------------------------*

* Include z_sample_lcl_airplane *

*----------------------------------------------------------------------*

**************************************** **

* Definition part

**************************************** **

Class lcl_airplane definition.

*------------------------------

* Public Section

*------------------------------

Public section.

Types: t_name (25) type C.

Methods:

      constructor    importing p2_name      type t_name
                               p2_planetype  TYPE saplane-planetype,

Set_attributes importing p_name type t_name

P_planetype type saplane-planetype,

Display_attributes,

Display_n_o_airplanes.

*------------------------------

* Private Section

*------------------------------

Private section.

* Private attributes

Data: Name (25) type C,

Planetype type saplane-planetype.

* Private Static attribute

Class-data n_o_airplanes type I.

Endclass.

**************************************** **

* Implementation part

**************************************** **

Class lcl_airplane implementation.

Method constructor.

* Counts number of instances

N_o_airplanes = n_o_airplanes + 1.

    name      = p2_name.
    planetype = p2_planetype.

Endmethod.

Method set_attributes.

Name = p_name.

Planetype = p_planetype.

Endmethod.

Method display_attributes.

Write:/'name: ', name, 'planetype:', planetype.

Endmethod.

Method display_n_o_airplanes.

Write:/'No. Planes: ', n_o_airplanes.

Endmethod.

Endclass.

Bytes ---------------------------------------------------------------------------------------------

Report zbobo_oo_011.

Include z_sample_lcl_airplane.

* Create reference to class lcl_airplane

Data: airplane1 type ref to lcl_airplane,

Airplane2 type ref to lcl_airplane.

Start-of-selection.

* Create instance

Create object airplane1

Exporting p2_name = 'airp1'

P2_planetype = 'bobo '.

Call method: airplane1-> display_n_o_airplanes.

Create object airplane2

Exporting p2_name = 'airp1'

P2_planetype = 'bobo '.

* Setting attributes using a method with Parameters

* Attributes of plane1 are modified, but those of plane2 are not changed.

Call method airplane1-> set_attributes exporting p_name = 'kurt'

P_planetype = 'md80 '.

End-of-selection.

* Using methods

* Display attributes that have been modified

Call method: airplane1-> display_n_o_airplanes,

Airplane1-> display_attributes.

* Initialized attributes

Call method: airplane2-> display_n_o_airplanes,

Airplane2-> display_attributes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.