这是脚本(Scccc.groovy):
import scriptParents.ScriptGroovyParent
println(queryThisBaby("my query"));
这是超类:
class ScriptGroovyParent {
public ScriptGroovyParent() {
// TODO Auto-generated constructor stub
}
// public String queryThisBaby(String query){
//
// return query +" was run.";
// }
def queryThisBaby(name) {
return name +" was run."
}
}
我在尝试运行脚本时遇到错误。
Caught: groovy.lang.MissingMethodException: No signature of method: scripts.Scccc.queryThisBaby() is applicable for argument types: (java.lang.String) values: [my query]
groovy.lang.MissingMethodException: No signature of method: scripts.Scccc.queryThisBaby() is applicable for argument types: (java.lang.String) values: [my query]
at scripts.Scccc.run(Scccc.groovy:5)
这怎么可能?
答案 0 :(得分:3)
脚本可以使用CompilerConfiguration扩展基类。需要注意的是,基类必须扩展Script,因为groovy脚本正常地扩展脚本,并且你不能在“IS A”关系中拥有多个继承。
//ScriptGroovyParent.groovy
abstract class ScriptGroovyParent extends Script{
def queryThisBaby(name) {
return name +" was run."
}
}
//Script Scccc.groovy
import org.codehaus.groovy.control.CompilerConfiguration
def configuration = new CompilerConfiguration()
configuration.setScriptBaseClass("ScriptGroovyParent")
def shell = new GroovyShell(this.class.classLoader, new Binding(), configuration)
assert shell.evaluate("queryThisBaby('My Query')") == 'My Query was run.'
如果包都位于不同的包中,则可以导入包。