2022 Apr 07 6:51 PM
This question was a temporary bug of ADT.
2022 Apr 07 7:00 PM
In my opinion your first implementation should be preferred as it reduces the member access. Pay attention that you cannot write your member from outside as it is declared read only.
What should work:
data(lo_instance) = new zcl_x( ).
write lo_instance->my_interface_string.
2022 Apr 07 7:05 PM
doing so in eclipse, I get my_interface_string with an underline and the error message "Field "MY_INTERFACE_STRING" is unknown."
2022 Apr 08 7:57 AM
At first, an Interface is a contract between a class & the outside.
This contract is here to expose a set of public methods regarding the same logic.
A class should use one or several interfaces to exchange with the outside (using public method in class is bad)
So now, looking your code, what is the responsibility of your interface ? store value ? an interface is not instantiable. So why trying to do this ?
Your interface, should have setter & getter method, and not the way to store the value, This is the responsibility of the class.
Nevertheless, the answer of your question is:
INTERFACE zif_x.
DATA: my_interface_string TYPE string. " READ-ONLY.
ENDINTERFACE.
CLASS zcl_x DEFINITION
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES zif_x.
METHODS: constructor.
ENDCLASS.
CLASS zcl_x IMPLEMENTATION.
METHOD constructor.
zif_x~my_interface_string = 'hello'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA lo_ref TYPE REF TO zif_x.
lo_ref = NEW zcl_x( ).
write lo_ref->my_interface_string.
DATA lo_ref2 TYPE REF TO zcl_x.
lo_ref2 = NEW #( ).
write lo_ref2->zif_x~my_interface_string.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |