ABAP,Java和JavaScript的local class

简介: Local class in ABAP

Suppose I have a global class with a public method ADD with following signature. I would like to implement with a local class inside this global class.



image.pngThe local class could be created by clicking button “Local Definitions/Implementions”:image.pngNow in my global class I can just delegate the ADD implementation to the local class. Notice that even ADD method is marked as public, it is still displayed as a red light in class builder, which makes sense since this ADD method in local class is not visible to outside consumers.


image.pngLocal class in ABAP in widely used in the following scenarios:


(1) ABAP unit test class

(2) The pre/post exit enhancement of class method are technically implemented via a local class in enhancement include.

For example, once you click the “Post-Exit” button below,



image.pngYou will see the source code as below, the local class LCL_ZCL_JERRY_POSTEXIT is automatically generated by class builder.CLASS LCL_ZCL_JERRY_POSTEXIT DEFINITION DEFERRED.

CLASS CL_JERRY_TOOL DEFINITION LOCAL FRIENDS LCL_ZCL_JERRY_POSTEXIT.

CLASS LCL_ZCL_JERRY_POSTEXIT DEFINITION.

PUBLIC SECTION.

CLASS-DATA OBJ TYPE REF TO LCL_ZCL_JERRY_POSTEXIT. "#EC NEEDED

DATA CORE_OBJECT TYPE REF TO CL_JERRY_TOOL . "#EC NEEDED

INTERFACES  IPO_ZCL_JERRY_POSTEXIT.

 METHODS:

  CONSTRUCTOR IMPORTING CORE_OBJECT

    TYPE REF TO CL_JERRY_TOOL OPTIONAL.

ENDCLASS.

CLASS LCL_ZCL_JERRY_POSTEXIT IMPLEMENTATION.

METHOD CONSTRUCTOR.

 ME->CORE_OBJECT = CORE_OBJECT.

ENDMETHOD.

METHOD IPO_ZCL_JERRY_POSTEXIT~GET_QUERY_RESULT.

*"------------------------------------------------------------------------*

*" Declaration of POST-method, do not insert any comments here please!

*"

*"class-methods GET_QUERY_RESULT

*"  importing

*"    !IV_COL_WRAPPER type ref to CL_BSP_WD_COLLECTION_WRAPPER

*"  changing

*"    value(RV_RESULT) type ref to IF_BOL_ENTITY_COL . "#EC CI_VALPAR

*"------------------------------------------------------------------------*

**************  define your own post enhancement here!!! **************

ENDMETHOD.

ENDCLASS.(3) if an event handler in a given program is not intended to be reused by other programs, it could be defined as a local class within the program it is used – in this case it is not necessary to define a global class as event handler.


There are lots of such examples in ALV examples delivered by SAP, see program BCALV_GRID_DND_TREE as example.



image.pngInner Class in Java

The above example could simply be written in Java as well. In this example the inner class lcl_local does not follow Camel naming convention since I would like to highlight that it works exactly the same as the one in ABAP example.


public class LocalClassTest{

public int add(int var1, int var2){

 return new lcl_local().add(var1, var2);

}

private class lcl_local {  

 public int add(int var1, int var2){

  return var1 + var2;

 }

}

}Just exactly the same as in ABAP, the local class could not be used outside the class where it is defined.image.pngOne typical usecase of inner class in Java is the efficient implementation of a thread-safe singleton pattern, see sample code below:public class Example {

   private static class StaticHolder {

       static final MySingleton INSTANCE = new MySingleton();

   }

 

   public static MySingleton getSingleton() {

       return StaticHolder.INSTANCE;

   }

}“Local class” in JavaScript

I use double quote in title since the function object in JavaScript are not actual “class” concept in ABAP and Java.


Below source code is a simulation of private attributes & methods in JavaScript:



function createClass(conf){

   function _injectAttribute(fn){

       var prototype = fn.prototype;

       for(var publicName in publics){

           if(!publics.hasOwnProperty(publicName))

               continue;

           if(typeof publics[publicName]=="function")

               prototype[publicName] = function(publicName){

                   return function(){

                       return publics[publicName].apply(privates, arguments);

                   }

               }(publicName);

           else  

               prototype[publicName] = publics[publicName];

           if(!privates[publicName])

               privates[publicName] = prototype[publicName];

       }

       return fn;

   }

   var publics, privates;

       publics = conf.publics;

       privates = conf.privates || new Object();

   var fn = function(fn){

    return function(){

     return fn.apply(privates, arguments);

    };

   }(conf.constructor || new Function());

   return _injectAttribute(fn);

}

var MyClass = createClass({

   constructor:function(){

       console.log("constructor is called: " + this.message);

   },

   publics:{

       message:"Hello, World",

       sayJavaScript:function(){

           return this._message;

       },

       sayABAP:function(msg){

           return msg + ", " + this.ABAP();

       }

   },

   privates:{

       _message: "Hello, JavaScript",

       ABAP :function(){

           return "ABAP";

       }

   }

});

var myClassInstance = new MyClass();

console.log(myClassInstance.message);

console.log(myClassInstance.sayJavaScript());

console.log(myClassInstance.sayABAP("Hello"));

console.log(myClassInstance._message);

console.log(myClassInstance.ABAP());image.png

Testing shows it works as expected:

image.png

Local function in ES6

In ES6 it is possible to define a class via syntax suger class:class Developer {

   constructor(name, language) {

       this.workingLanguage = language;

       let _name = name;

       let _getName = function() {  

           return _name;  

       };

       this.getName = _getName;

   }

}

var Jerry = new Developer("Jerry", "Java");

var Ji = new Developer("Ji", "JavaScript");

console.log("Developer name: " + Jerry.getName());

console.log("Jerry's working language: " + Jerry.workingLanguage);

console.log("local function accessible? " + Jerry._getName);

console.log("Jerry's name: " + Jerry._name);image.pngimage.pngFurther reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection

Local class in ABAP, Java and JavaScript

Integer in ABAP, Java and JavaScript

Covariance in Java and simulation in ABAP

Various Proxy Design Pattern implementation variants in Java and ABAP

Tag(Marker) Interface in ABAP and Java

Bitwise operation ( OR, AND, XOR ) on ABAP Integer

ABAP ICF handler and Java Servlet


相关文章
|
9天前
|
Java
java基础(4)public class 和class的区别及注意事项
本文讲解了Java中`public class`与`class`的区别和注意事项。一个Java源文件中只能有一个`public class`,并且`public class`的类名必须与文件名相同。此外,可以有多个非`public`类。每个类都可以包含一个`main`方法,作为程序的入口点。文章还强调了编译Java文件生成`.class`文件的过程,以及如何使用`java`命令运行编译后的类。
15 3
java基础(4)public class 和class的区别及注意事项
|
9天前
|
Web App开发 JavaScript 前端开发
JavaScript 类(class)
JavaScript 类(class)
15 2
JavaScript 类(class)
|
9天前
|
Java
java的class类
java的class类
18 5
|
8天前
|
JavaScript 前端开发
js之class继承|27
js之class继承|27
|
2月前
|
Java 测试技术 Docker
记录一次很坑的报错:java.lang.Exception: The class is not public.
这篇文章记录了作者在Docker中运行服务进行单元测试时遇到的一系列问题,包括Spring Boot与Spring Cloud版本不一致、Bean注入问题、测试单元引入问题以及公共类和方法的可见性问题,并提供了解决问题的方法和成功测试通过的代码示例。
记录一次很坑的报错:java.lang.Exception: The class is not public.
|
2月前
|
Java
JAVA中public class和class的区别
JAVA中public class和class的区别
34 7
|
2月前
|
JavaScript 前端开发 Oracle
|
2月前
|
JavaScript 前端开发 Java
Java和JavaScript区别与联系
【8月更文挑战第18天】
|
2月前
|
Oracle Java 关系型数据库
简单记录在Linux上安装JDK环境的步骤,以及解决运行Java程序时出现Error Could not find or load main class XXX问题
本文记录了在Linux系统上安装JDK环境的步骤,并提供了解决运行Java程序时出现的"Error Could not find or load main class XXX"问题的方案,主要是通过重新配置和刷新JDK环境变量来解决。
74 0
|
3月前
|
Java
Error:Internal error: (java.lang.IllegalAccessError) class com.,idea2019.3版本,必须用application2.7.6或者以下
Error:Internal error: (java.lang.IllegalAccessError) class com.,idea2019.3版本,必须用application2.7.6或者以下
下一篇
无影云桌面