ABAP,Java和JavaScript的local class-阿里云开发者社区

开发者社区> 开发者小助手-bz4> 正文

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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
使用NAT网关轻松为单台云服务器设置多个公网IP
在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
26538 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
11009 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10470 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
21734 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
3742 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
6628 0
2315
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载