2 | Working with Objects
methods, types, and so on. It is possible to use the class component selector in
conjunction with an object reference variable (i.e., oref->c1ass_component).
However, this syntax can become confusing as you will see later in the book.
Therefore, this book sticks to the convention of binding the class component
selector to the class name. This convention emphasizes the fact that the compo-
nents are related to the class itself rather than a particular instance of the class.
If you look closely at class lcl_point, you will notice that the method
get_di stance has been redefined as a class method. Because get_distance is no
longer invoked on a particular instance of class lcl_point, the method signature
had to be changed to provide importing parameters for both points. Here, keep in
mind that class methods cannot implicitly access instance attributes or instance
methods within the class because there is no me self-reference associated with the
static context of the class. Rather, class methods can only access instance
attributes through object reference variables created locally or passed in as input
parameters. Class methods can, of course, access other class methods and
attributes without any such restrictions.
The example code shown in Listing 2.17 illustrates some of the updated features
of class lcl_point. First, four object instances have been created: lr_po1nt_a,
lr_point_b,
1
r_point_c, and
1
r_point_d. Each time an object is created, the con-
structor method is invoked, and a unique point number is assigned using the
class attribute next_point_no. This functionality is evidenced by the call to
method get_po1nt_number for the point referenced by
1
r_po1nt_c, which should
produce the expected value 3. Similarly, the call to method get_quadrant should
determine that the point referenced by
1
r_point_c is located in Quadrant 3 based
on the assigned x- and y-coordinate values. Finally, the call to class method
get_distance calculates the distance between the points referenced by
lr_point_c and
1
r_point_d. Here, notice the use of the class component selector
in the lcl_point->get_di stance method call.
OATA: 1r_point_a TYPE REF TO lcl_point.
1r_point_b TYPE REF TO lcl_point,
1r_point_C TYPE REF TO lcl_point.
1r_point_d TYPE REF TO lcl.point.
1v_point_number TYPE numcl.
lv_quadrant TYPE numcl.
lv_distance TYPE f.
60