class ZCL_POINT definition
public
final
create public .
public section.
data X type I .
methods CONSTRUCTOR
importing
!IV_X type I
!IV_Y type I .
private section.
data Y type I .
class-data COUNT type I .
ENDCLASS.
CLASS ZCL_POINT IMPLEMENTATION.
method CONSTRUCTOR.
me->x = iv_x.
me->y = iv_y.
count = count + 1.
endmethod.
ENDCLASS.
data(a) = new zcl_point( iv_x = 1 iv_y = 1 ).
data(b) = new zcl_point( iv_x = 1 iv_y = 2 ).
data(c) = new zcl_point( iv_x = 1 iv_y = 3 ).
data(d) = new zcl_point( iv_x = 1 iv_y = 4 ).
data(lo) = CAST cl_abap_objectdescr( cl_abap_classdescr=>describe_by_name( 'ZCL_POINT' ) ).
read TABLE lo->attributes ASSIGNING FIELD-SYMBOL(<count>) WITH KEY name = 'COUNT'.
CHECK SY-SUBRC = 0.
<count>-visibility = 'U'.
import java.lang.reflect.Field;
public class Point {
private int x;
private int y;
static private int count = 0;
public Point(int x, int y){
this.x = x;
this.y = y;
count++;
}
private static void accessStaticPrivate(Point point){
Class classObject = point.getClass();
try {
Field countField = classObject.getDeclaredField("count");
System.out.println("count: " + countField.get(point));
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException
| IllegalAccessException e1 ) {
e1.printStackTrace();
}
}
public static void main(String[] arg){
Point a = new Point(1,2);
accessStaticPrivate(a);
Point b = new Point(1,3);
accessStaticPrivate(b);
Point c = new Point(1,4);
accessStaticPrivate(c);
Point d = new Point(1,5);
accessStaticPrivate(d);
}
}
count: 1
count: 2
count: 3
count: 4
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
11 | |
10 | |
10 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 |