Groovy的脚本统一于类的世界
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:
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
runmethod 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 scriptif 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的脚本统一于类的世界的更多相关文章
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...
- 远程shell脚本执行工具类
/** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...
- springboot统一异常处理类及注解参数为数组的写法
统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...
- Android 开发工具类 10_Toast 统一管理类
Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...
- Android 开发工具类 05_Logcat 统一管理类
Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...
- Log统一管理类
import android.util.Log; /** * Log统一管理类 */ public class L{ private L(){ /* cannot be instantiated */ ...
- IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)
IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...
- 用 CallerMemberName Attribute 和 EqualityComparer 统一处理类的属性值变化
当需要实现类似 INotifyPropertyChanged 这样的接口的时候,每一个属性去判断值是否变化,然后触发事件什么的,太麻烦了,如果能用一个方法统一处理就好了. 好在真的可以做到.这个博文说 ...
- javascript王国的一次旅行,一个没有类的世界怎么玩转面向对象?
1. 前言 作为Java 帝国的未来继承人,Java小王子受到了严格的教育, 不但精通Java语言.Java虚拟机.java类库和框架,还对各种官方的Java规范了如指掌. 近日他听说一个叫做Java ...
随机推荐
- nginx php-fpm conf文件编写
coco.conf ##upstream upstream php_coco_backend{ server 127.0.0.1:8019; } server { listen 80; server_ ...
- 洛谷P3354 河流
有点权边权的树,选出k个关键点,根必须选.每个点的贡献为点权 * 到最近的关键祖先的距离.求最小总贡献. 解:树形DP是最毒瘤的算法...... 设fxij表示以x为根的子树中选了j个关键点,且x的最 ...
- 【CF1141G】Privatization of Roads in Treeland
题目大意:给定一个 N 个点的无根树,现给这个树进行染色.定义一个节点是坏点,若满足与该节点相连的至少两条边是相同的颜色,求至多有 k 个坏点的情况下最少需要几种颜色才能进行合法染色. 题解:考虑一个 ...
- Django(一)自定义web框架
https://www.cnblogs.com/yuanchenqi/articles/6083427.htm 一 什么是web框架 框架,即framework, 特指为解决一个开放性问题而设计的具有 ...
- Linux编译安装PHP参数说明
php各参数配置详解 --prefix=/usr/local/php //指定 php 安装目录 --with-apxs2=/usr/local/apache/bin/apxs //整合apache, ...
- Python之函数--命名空间、作用域、global、nonlocal、函数的嵌套和作用域链
命名空间 -------‘’存放名字与值的关系”的空间 代码在运行伊始,创建的存储“变量名与值的关系”的空间叫做全局命名空间: 在函数的运行中开辟的临时的空间叫做局部命名空间. 命名空间一共分为三种: ...
- Day032--Python--操作系统, process进程
多道技术背景: 提高工作效率(充分利用I/O阻塞的时间) (I: input, O: output) 同时执行多个任务 多道技术: 空间复用: 充分利用内存空间 时间复用: 充分利用I/O阻塞时 ...
- 1. github配置
1. 安装:官网傻瓜式安装 2.密钥的生成:为了不让不想干的人提交代码,所以需要一个密钥 执行这个命令 : ssh-keygen -t rsa -C "邮箱地址" 然后一直回车键回 ...
- 踩过的坑—iphone手机H5样式兼容总结
对一个前端开发者来说,最煎熬的莫过于"兼容"两个字了(说到这个词朋友们是不是身体一抖),哪怕对于工作多年的老油条来讲,也不是完全了解各种场景下的兼容性处理方法.在这里我就把我在工作 ...
- 【清北学堂2018-刷题冲刺】Contest 4
Task 1:序列 [问题描述] 小H原本有一个由连续的正整数组成的序列,如{4,5,6}或{10,11,12,13,14,15,16},但是她最近睡眠不足,只能记得其中的一些数字.她想知道,她最少 ...