程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> atitit.bsh BeanShell 的動態腳本使用java,beanshell

atitit.bsh BeanShell 的動態腳本使用java,beanshell

編輯:JAVA綜合教程

atitit.bsh BeanShell 的動態腳本使用java,beanshell


atitit.bsh BeanShell 的動態腳本使用java

 

 

1.1. BeanShell是一個小巧免費的JAVA源碼解釋器

,支持對象式的腳本語言特性,亦可嵌入到JAVA源代碼中。

亦可嵌入到JAVA源代碼中,能動態執行JAVA源代碼並為其擴展了腳本語言的一些特性,像JavaScript和perl那樣的弱類型、命令式、閉包函數等等特性都不在話下

 

BeanShell能理解標准的JAVA語句,表達式,和方法宣告。語句和表達式的內容可以是:變量,宣告,賦值,方法調用,循環,條件等。
在 Java程序中你必須嚴格的使用它們,但在BeanShell中,你可以用“寬松類型”(loosely typed)的方式來使用它們。也就是說,你可以在寫腳本時偷懶,不進行變量類型的宣告(在原始數據類型和對象都可以)。如果你試著用錯變量類 型,BeanShell將會給出一個錯誤。

作者:: 綽號:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿爾 拉帕努伊 ) 漢字名:艾龍,  EMAIL:[email protected]

轉載請注明來源: http://www.cnblogs.com/attilax/

 

2. 運行方式:

l 界面UI方式 :java bsh.Console

l 命令行方式 :java bsh.Interpreter

l運行腳本文件:java bsh.Interpreter filename [ args ]

3. BeanShell將成為Java平台上的第三種編程語言

JCP接納了一個新的技術規范進入標准化進程,這個編號為JSR-274的技術規范將把BeanShell引入為Java平台上支持的又一種編程語言。

JSR- 274(http://jcp.org/en/jsr/detail?id=274)是由 Patrick Niemeyer提交的技術規范,其目標是將BeanShell腳本語言(http://www.beanshell.org/)規范化為Java虛擬機 平台上支持的第三種編程語言。除了Java之外,Java虛擬機還支持Groovy腳本語言。Doug Lea、Apache和Google三個JCP執委會成員對此規范表示了支持。

按照Java最初的設計思路,有很多語言都可以在JVM上 運行(詳細列表參見http://en.wikipedia.org/wiki/List_of_Java_scripting_languages), 但這些語言大多沒有流行起來。直到2004年為止,Java平台事實上只有一種編程語言,也就是Java。2004年3月,Groovy(JSR- 241)成為了Java平台上的第二種編程語言。

3.1. Define  method

3.2. Scripted Methods

You can declare and use methods in BeanShell just as you would in a Java class.

 

int addTwoNumbers( int a, int b ) {

    return a + b;

}

 

sum = addTwoNumbers( 5, 7 );  // 12

 

Bsh methods may also allow dynamic (loose) argument and return types.

 

add( a, b ) {

    return a + b;

}

 

foo = add(1, 2);            // 3

foo = add("Oh", " baby");   // "Oh baby"

 

3.3. Implementing Interfaces

Note: implementing arbitrary interfaces requires BeanShell be run under a Java 1.3 or higher environment.

You can use the standard Java anonymous inner class syntax to implement an interface type with a script. For example:

 

ActionListener scriptedListener = new ActionListener() {

    actionPerformed( event ) { ... }

}

 

You don't have to script all of the methods of an interface. You can opt to script only those that you intend to call if you want to. The calling code will simply throw an exception if it tries to invoke a method that isn't defined. If you wish to override the behavior of a large number of methods - say to produce a "dummy" adapter for logging - you can implement a special method signature: invoke(name, args) in your scripted object. The invoke() method is called to handle any undefined method invocations:

 

ml = new MouseListener() {

    mousePressed( event ) { ... }

    // handle the rest

    invoke( name, args ) { print("Method: "+name+" invoked!");

}

 

 

4. Refe

 

BeanShell(JAVA源碼解釋器)_百度百科.htm

BeanShell快速入門---Java應用程序腳本引擎 - Love program - BlogJava.htm

Quick Start.htm

Embedding BeanShell in Your Application.htm

BeanShell Commands Documentation.htm

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved