前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ABAP,Java和JavaScript的local class

ABAP,Java和JavaScript的local class

作者头像
Jerry Wang
发布2020-08-24 11:45:47
4890
发布2020-08-24 11:45:47
举报

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.

The local class could be created by clicking button “Local Definitions/Implementions”:

Now 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.

Local 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,

You will see the source code as below, the local class LCL_ZCL_JERRY_POSTEXIT is automatically generated by class builder.

代码语言:javascript
复制
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.

Inner 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.

代码语言:javascript
复制
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.

One typical usecase of inner class in Java is the efficient implementation of a thread-safe singleton pattern, see sample code below:

代码语言:javascript
复制
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:

代码语言: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());

Testing shows it works as expected:

Local function in ES6

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

代码语言:javascript
复制
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);

Further 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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Local class in ABAP
  • Inner Class in Java
  • “Local class” in JavaScript
  • Local function in ES6
  • Further reading
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档