Files
abap/concepts/basics/introduction.md
Marc Bernard fe1956dbb1 Add docs for Lasagne exercise & Basics concept (#283)
* Add docs for Lasagne exercise

* More docs

---------

Co-authored-by: Lars Hvam <larshp@hotmail.com>
2024-04-07 20:03:02 +02:00

2.8 KiB

Introduction

ABAP supports an object-oriented programming model that is based on classes and interfaces of ABAP Objects.

(Re-)Assignment

There are a few primary ways to assign values to names in ABAP - using variables or constants. On Exercism, variables are always written in [snake-case][wiki-snake-case]. There is no official guide to follow, and various companies and organizations have various style guides. Feel free to write variables any way you like. The upside from writing them the way the exercises are prepared is that they'll be highlighted differently in the web interface and most IDEs.

Variables in ABAP can be defined using the constant or data keywords.

A variable can reference different values over its lifetime when using data. For example, my_first_variable can be defined and redefined many times using the assignment operator =:

DATA my_first_variable TYPE i. " integer

my_first_variable = 1.
my_first_variable = 4711 * 3.
my_first_variable = some_complex_calculation( ).

In contrast to data, variables that are defined with constant can only be assigned once. This is used to define constants in ABAP.

CONSTANT my_first_constant TYPE i VALUE 10.

" Can not be re-assigned
my_first_constant = 20.
// => SyntaxError: Assignment to constant variable.

Class and Method Declarations

In ABAP, units of functionality are encapsulated in methods, usually grouping methods together in the same class if they belong together. These methods can take parameters (arguments), and can return a value using the returning keyword in the method definition. Methods are invoked using ( ) syntax.

CLASS my_class DEFINITION.

  PUBLIC SECTION.

    METHODS add
      IMPORTING
        num1          TYPE i
        num2          TYPE i
      RETURNING
        VALUE(result) TYPE i.

ENDCLASS.

CLASS my_class IMPLEMENTATION.

  METHOD add.
    result = num1 + num2.
  ENDMETHOD.

ENDCLASS.

add( num1 = 1 num2 = 3 ).
// => 4

💡 In ABAP there are many different ways to declare classes and methods. The track tries to gradually introduce them, but if you already know about them, feel free to use any of them. In most cases, using one or the other isn't better or worse.