Thursday, December 29, 2011

Read only in oo abap


READ-ONLY

Effect

This addition is solely possible in the public visibility area of a class or in an interface. It has the effect that an attribute declared using DATA is readable from an external point, but can only be changed using methods of the class or its subclasses.
A class attribute defined using READ-ONLY can be used outside of the class only at read positions in ABAP statements.

Note

The declaration of attributes with the READ-ONLY addition does not prevent methods of the references class for these attributes from getting outside in the form of reference variables or field symbols, in which case the attributes could thus be changed outside of the class.
Sample

*&---------------------------------------------------------------------*
*& Report  YSAP_OOABAP_READ_ONLY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  YSAP_OOABAP_READ_ONLY.


class class1 definition.
  PUBLIC SECTION.
  data: pernr type pa0002-pernr READ-ONLY.
  methods: constructor importing p_pernr type i.
  endclass.

  class class1 implementation.
    method constructor.
      me->pernr = p_pernr.
      endmethod.
      endclass.

START-OF-SELECTION.
  DATA: test TYPE REF TO class1.
  CREATE OBJECT test
    EXPORTING
      p_pernr = 10.
  WRITE: 'pernr:', test->pernr. 



 

No comments:

Post a Comment