http://groovy-lang.org/structure.html

3.2. Script class

A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method. The previous example is therefore compiled as if it was the following:

Main.groovy
import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
def run() {
println 'Groovy world!'
}
static void main(String[] args) {
InvokerHelper.runScript(Main, args)
}
}
  The Main class extends the groovy.lang.Script class
  groovy.lang.Script requires a run method returning a value
  the script body goes into the run method
  the main method is automatically generated
  and delegates the execution of the script on the run method

If the script is in a file, then the base name of the file is used to determine the name of the generated script class. In this example, if the name of the file is Main.groovy, then the script class is going to be Main.

3.3. Methods

It is possible to define methods into a script, as illustrated here:

int fib(int n) {
n < 2 ? 1 : fib(n-1) + fib(n-2)
}
assert fib(10)==89

You can also mix methods and code. The generated script class will carry all methods into the script class, and assemble all script bodies into the run method:

println 'Hello'                                 

int power(int n) { 2**n }                       

println "2^6==${power(6)}"
  script begins
  a method is defined within the script body
  and script continues

This code is internally converted into:

import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
int power(int n) { 2** n}
def run() {
println 'Hello'
println "2^6==${power(6)}"
}
static void main(String[] args) {
InvokerHelper.runScript(Main, args)
}
}
  the power method is copied as is into the generated script class
  first statement is copied into the run method
  second statement is copied into the run method
  Even if Groovy creates a class from your script, it is totally transparent for the user. In particular, scripts are compiled to bytecode, and line numbers are preserved. This implies that if an exception is thrown in a script, the stack trace will show line numbers corresponding to the original script, not the generated code that we have shown.

3.4. Variables

Variables in a script do not require a type definition. This means that this script:

int x = 1
int y = 2
assert x+y == 3

will behave the same as:

x = 1
y = 2
assert x+y == 3

However there is a semantic difference between the two:

  • if the variable is declared as in the first example, it is a local variable. It will be declared in the run method that the compiler will generate and will not be visible outside of the script main body. In particular, such a variable will not be visible in other methods of the script

  • if the variable is undeclared, it goes into the script binding. The binding is visible from the methods, and is especially important if you use a script to interact with an application and need to share data between the script and the application. Readers might refer to the integration guide for more information.

  If you want a variable to become a field of the class without going into the Binding, you can use the @Field annotation.

Groovy的脚本统一于类的世界的更多相关文章

  1. Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

    在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...

  2. 远程shell脚本执行工具类

    /** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...

  3. springboot统一异常处理类及注解参数为数组的写法

    统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...

  4. Android 开发工具类 10_Toast 统一管理类

    Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...

  5. Android 开发工具类 05_Logcat 统一管理类

    Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...

  6. Log统一管理类

    import android.util.Log; /** * Log统一管理类 */ public class L{ private L(){ /* cannot be instantiated */ ...

  7. IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)

    IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...

  8. 用 CallerMemberName Attribute 和 EqualityComparer 统一处理类的属性值变化

    当需要实现类似 INotifyPropertyChanged 这样的接口的时候,每一个属性去判断值是否变化,然后触发事件什么的,太麻烦了,如果能用一个方法统一处理就好了. 好在真的可以做到.这个博文说 ...

  9. javascript王国的一次旅行,一个没有类的世界怎么玩转面向对象?

    1. 前言 作为Java 帝国的未来继承人,Java小王子受到了严格的教育, 不但精通Java语言.Java虚拟机.java类库和框架,还对各种官方的Java规范了如指掌. 近日他听说一个叫做Java ...

随机推荐

  1. Ubuntu16.04下的NetCore环境搭建(附录含Ubuntu 18.04 安装 NetCore2.1)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux VSCode安装:http://www.cnblogs.com/dunitia ...

  2. 移动端利用-webkit-box水平垂直居中(旧弹性盒)

    新弹性盒水平垂直居中参考:http://www.cnblogs.com/ooo0/p/7562884.html 新旧弹性盒样式参考:http://www.cnblogs.com/ooo0/p/7562 ...

  3. Nginx概述及安装配置

    1.概述 Nginx是一款开源的,高性能的Http和反向代理服务器,同时也是一个IMAP.POP3.SMTP代理服务器:Nginx可以作为一个HTTP服务器进行网站的发布处理,也可以作为反向代理进行负 ...

  4. 第五篇 - Selenium突破反爬获取qq邮件标题

    from selenium import webdriver from selenium.webdriver import ActionChains #1.打开登陆页面 wd = webdriver. ...

  5. mysql数据库的优化和查询效率的优化

    一.数据库的优化 1.优化索引.SQL 语句.分析慢查询: 2.设计表的时候严格根据数据库的设计范式来设计数据库: 3.使用缓存,把经常访问到的数据而且不需要经常变化的数据放在缓存中,能节约磁盘IO: ...

  6. redis设置最大内存

  7. Image.fromarray的用法

    简而言之,就是实现array到image的转换 详细参考以下博客 https://blog.csdn.net/ybcrazy/article/details/81206411

  8. mysql 存储过程中文乱码的解决方法

    CREATE PROCEDURE `PROC_FOOBAR`(id INTEGER) BEGIN DECLARE code, user_id VARCHAR(32) CHARACTER SET utf ...

  9. xrange与range的区别

    xrange属于类,继承自object. 用法: xrange(stop) -> xrange object xrange(start, stop[, step]) -> xrange o ...

  10. icmpsh解决运行报错

    运行./run.sh报错 sysctl -w net.ipv4.icmp_echo_ignore_all=1 >/dev/null python icmpsh_m.py 攻击机ip 目标机ip ...