SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Статический и инстанс конструктор



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP
View previous topic :: View next topic  
Author Message
stn
Участник
Участник



Joined: 18 Jan 2008
Posts: 31

PostPosted: Fri Feb 08, 2008 4:28 pm    Post subject: Статический и инстанс конструктор Reply with quote

Привет!
Пытаюсь разобраться с работой классов. Сделал небольшую программу с тремя классами C1, C2.
Класс C2 наследуется от C1.
Какая последовательность вызова статического и инстанс конструкторов классов C1 и С2?
Back to top
View user's profile Send private message
Lord
Профессионал
Профессионал



Joined: 10 Sep 2007
Posts: 168

PostPosted: Mon Feb 11, 2008 1:28 pm    Post subject: Reply with quote

Вот немного модифицированный пример из курса BC404

Quote:
 The static constructor is a special static method in a class and is always named CLASS_CONSTRUCTOR. It is executed precisely once per program. The static constructor of class <classname> is called automatically before the class is first accessed, that is, before any of the following actions are executed:

­ Creating an instance in the class using CREATE OBJECT obj, where obj has the data type
REF TO <classname>.
­ Addressing a static attribute using <classname>=><an_attribute>.
­ Calling a static attribute using CALL METHOD <classname>=><a_classmethod>.
­ Registering a static event handler method using SET HANDLER <classname>=><handler_method> for obj.
­ Registering an event handler method for a static event in class <classname>.
 The static constructor cannot be called explicitly.

Code:
CLASS lcl_airplane DEFINITION.

  PUBLIC SECTION.

    TYPES: name_type(25) TYPE c.
    CONSTANTS: pos_1 TYPE i VALUE 30.

*   Constructor
    METHODS: constructor importing
                         im_name      TYPE name_type
                         im_planetype TYPE saplane-planetype,
             set_attributes IMPORTING
                 im_name      TYPE name_type
                            im_planetype TYPE saplane-planetype,
             display_attributes.

    CLASS-METHODS: display_n_o_airplanes,
*   Static Constructor
                   class_constructor.

  PRIVATE SECTION.

    DATA: name      TYPE name_type,
          planetype TYPE saplane-planetype.

    CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS.


*------------------------------------------------------------------*
*       CLASS lcl_airplane IMPLEMENTATION                          *
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.

* Static constructor
  METHOD class_constructor.
    n_o_airplanes = n_o_airplanes + 1.
    WRITE: / 'Static Constructor of lcl_airplane'.
  ENDMETHOD.

* Constructor
  METHOD constructor.
    name          = im_name.
    planetype     = im_planetype.
    n_o_airplanes = n_o_airplanes + 1.
    WRITE: / 'Instance Constructor of Class lcl_airplane'.
  ENDMETHOD.

  METHOD set_attributes.
    name      = im_name.
    planetype = im_planetype.
*   n_o_airplanes = n_o_airplanes + 1.
  ENDMETHOD.

  METHOD display_attributes.
    WRITE: / 'Name of the airplane: '(001), at pos_1 name,
           / 'Plane type:           '(002), at pos_1 planetype.
  ENDMETHOD.

  METHOD display_n_o_airplanes.
    WRITE: /, / 'Total number of airplanes: '(ca1),
           AT pos_1 n_o_airplanes left-justified, /.
  ENDMETHOD.

ENDCLASS.

*------------------------------------------------------------------*
*       CLASS lcl_cargo_airplane DEFINITION                        *
*------------------------------------------------------------------*
CLASS lcl_cargo_airplane DEFINITION INHERITING FROM lcl_airplane.

  PUBLIC SECTION.

    METHODS: constructor IMPORTING
                         im_name      TYPE name_type
                         im_planetype TYPE saplane-planetype
                         im_cargo_max TYPE p,
            display_attributes REDEFINITION.

    CLASS-METHODS:
*   Static Constructor
                   class_constructor.

  PRIVATE SECTION.

    DATA: cargo_max TYPE scplane-cargomax.

ENDCLASS.

*------------------------------------------------------------------*
*       CLASS lcl_cargo_airplane IMPLEMENTATION                    *
*------------------------------------------------------------------*
CLASS lcl_cargo_airplane IMPLEMENTATION.

* Static constructor
  METHOD class_constructor.
    WRITE: / 'Static Constructor of lcl_cargo_airplane'.
  ENDMETHOD.

  METHOD constructor.
    CALL METHOD super->constructor EXPORTING
                                   im_name      = im_name
                                   im_planetype = im_planetype.
    cargo_max = im_cargo_max.
    WRITE: / 'Instance Constructor of Class lcl_cargo_airplane'.

  ENDMETHOD.

  METHOD display_attributes.
    CALL METHOD super->display_attributes.
    WRITE: / 'Maximal cargo:      '(004),
           at pos_1 cargo_max left-justified, /.
  ENDMETHOD.

ENDCLASS.

****************************************************
DATA: airplane TYPE REF TO lcl_airplane,
      cargo_airplane TYPE REF TO lcl_cargo_airplane.

START-OF-SELECTION.

  CALL METHOD lcl_airplane=>display_n_o_airplanes.

  CREATE OBJECT airplane EXPORTING
                                       im_name      = 'LH New York'
                                       im_planetype = '747-400'.

  CREATE OBJECT cargo_airplane EXPORTING
                               im_name      = 'US Hercules'
                               im_planetype = 'Galaxy'
                               im_cargo_max = 30000.


*  CALL METHOD airplane->display_attributes.

  CALL METHOD lcl_airplane=>display_n_o_airplanes.

Результат:
Quote:
Static Constructor of lcl_airplane

Total number of airplanes: 1

Instance Constructor of Class lcl_airplane
Static Constructor of lcl_cargo_airplane
Instance Constructor of Class lcl_airplane
Instance Constructor of Class lcl_cargo_airplane

Total number of airplanes: 3
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.