且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Groovy脚本中功能的“包含"

更新时间:2023-12-05 14:48:10

无需编译groovy脚本,您可以包括一个定义为类的脚本.

There is no need to compile the groovy script, you can include a script defined as a class just fine.

获取文件SomeClass.groovy

Take a file SomeClass.groovy

class SomeClass {
    def add(a,b){
        return a+b
    }
}

和脚本SomeScript.groovy

and a script SomeScript.groovy

println(new SomeClass().add(1,1))

只要SomeClass.groovy在CLASSPATH上,此方法就可以使用.

This will work as long as SomeClass.groovy is on the CLASSPATH.

编辑

class SomeClass {
    def static add(a,b){
        return a+b
    }
}

呼叫方式:

println(SomeClass.add(1,1))