打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CLASS-METHODS 
userphoto

2014.01.06

关注

CLASS-METHODS 

CLASS-METHODS Syntax Diagram

Variants:


1. CLASS-METHODS meth.
2. CLASS-METHODS meth FOR EVENT evt OF cif.

Effect

Declares static methods in a class or interface in ABAP Objects.

You can only use this statement in the declaration part of a class definition (see CLASS) or in an interface definition (see INTERFACE ). The CLASS- prefix to the METHODS statement means that the method meth is declared as a static method. Otherwise, the CLASS-METHODS statement has the same syntax as the METHODS statement for instance methods.

Static methods belong to the

static components of a class. Unlike instance methods, which can access both instance and static attributes, and trigger both instance and static events, static methods can only access static attributes (see CLASS-DATA ), and can only trigger static events (see CLASS-EVENTS). This makes static methods independent of instances, and callable even when no instances of a class exist. Static methods work like global functions, restricted to the context of a class. You use them to work with the contents of static attributes, or for tasks that affect the entire system.

Within a class, static methods have the same appearance as instance methods, and can be addressed in the same way. They can also be addressed from outside the class if their visibility allows. When you address a static method from outside its class, you can use either an object reference or the class name. For example, if meth is a static method of the class class, you can address it using CALL METHOD class=>meth in any program in which the class class is declared.

Note

If the class has a

static constructor (a method called CLASS_CONSTRUCTOR) that has not yet been executed in the program when you call a static method, it is executed bef ore the first static method call or the first registration of a static method as an event handler.

Effect

This variant corresponds to the first variant of the METHODS statement. You can use all of the additions listed in the first variant of the METHODS statement with the CLASS-METHODS statement.

Example

CLASS C1 DEFINITION.   PUBLIC SECTION.     METHODS CONSTRUCTOR.     CLASS-METHODS CREATE_COUNT_RESET.   PRIVATE SECTION.     CLASS-DATA CREATE_COUNT TYPE I. ENDCLASS.  DATA: O1 TYPE REF TO C1,       O2 LIKE O1,       O3 LIKE O1.  CREATE OBJECT: O1,                O2,                O3.  CLEAR: O1,        O2,        O3.  CALL METHOD C1=>CREATE_COUNT_RESET.  CREATE OBJECT: O1,                O2.  CLASS C1 IMPLEMENTATION.   METHOD CONSTRUCTOR.     CREATE_COUNT = CREATE_COUNT + 1.   ENDMETHOD.   METHOD CREATE_COUNT_RESET.     CLEAR CREATE_COUNT.   ENDMETHOD. ENDCLASS. 

This example builds on the example in the CLASS-DATA documentation. A private static attribute CREATE_COUNT counds the number of objects created in class C1. Once all reference variables have been initialized using the CLEAR statement, no more references exist to the objects in class C1, so these are automatically deleted (garbage collection). You can use the static method CREATE_COUNT_RESET to initialize the static attribute CREATE_COUNT even though there are no more instances of the class C1.

Effect

This variant corresponds to the second variant in the METHODS statement documentation. All of the additions for this variant of the METHODS statement can also be used in the CLASS-METHODS statement.

Like instance methods, static methods can also be registered as handlers for an event. This means that you do not then need an instance to handle the event.

Example

CLASS C1 DEFINITION DEFERRED.  CLASS C2 DEFINITION.   PUBLIC SECTION.     METHODS: CREATE,              DEL_OBJ.     EVENTS OBJ_DELETED.   PRIVATE SECTION.     DATA: O1 TYPE REF TO C1,           O2 LIKE O1,           O3 LIKE O1. ENDCLASS.  CLASS C1 DEFINITION.   PUBLIC SECTION.     METHODS CONSTRUCTOR.     CLASS-METHODS CREATE_COUNT_RESET                   FOR EVENT OBJ_DELETED OF C2.   PRIVATE SECTION.     CLASS-DATA CREATE_COUNT TYPE I. ENDCLASS.  DATA OBJ TYPE REF TO C2.  CREATE OBJECT OBJ.  SET HANDLER C1=>CREATE_COUNT_RESET FOR OBJ.  CALL METHOD OBJ->CREATE. CALL METHOD OBJ->DEL_OBJ.  CLASS C1 IMPLEMENTATION.   METHOD CONSTRUCTOR.     CREATE_COUNT = CREATE_COUNT + 1.   ENDMETHOD.   METHOD CREATE_COUNT_RESET.     CLEAR CREATE_COUNT.   ENDMETHOD. ENDCLASS.  CLASS C2 IMPLEMENTATION.   METHOD CREATE.     CREATE OBJECT: O1,                    O2,                    O3.   ENDMETHOD.   METHOD DEL_OBJ.     CLEAR: O1,            O2,            O3.     RAISE EVENT OBJ_DELETED.   ENDMETHOD. ENDCLASS. 

This example creates and then deletes instances of the class C1 in an object OBJ of class C2 . The static method from the previous example - CREATE_COUNT_RESET, is defined here as an event handler method for the event OBJ_DELETED of class C2 . The event can trigger the static method, even though no more instances of class C1 exist.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
How Model View Controller (MVC) Concept used in ABAP? |
深入理解Java类型信息(Class对象)与反射机制
Java反射机制
C -> Cs: What You Need to Know to Move from C to Cs
自制IOC容器(2)
java 反射机制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服