[英]Can a groovy script inherit from a Java or Groovy superclass?
Here is the script (Scccc.groovy): 这是脚本(Scccc.groovy):
import scriptParents.ScriptGroovyParent
println(queryThisBaby("my query"));
and here is the superclass: 这是超类:
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."
}
}
I get an error though when trying to run the script. 我在尝试运行脚本时遇到错误。
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)
How can this be? 怎么会这样?
A script can extend a base class using CompilerConfiguration . 脚本可以使用CompilerConfiguration扩展基类。 The caveat here is that base class has to extend Script as groovy scripts extend Script normally and you cannot have multiple inheritance in a "IS A" relationship.
需要注意的是,基类必须扩展脚本,因为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.'
You can import the package if they both reside in different packages. 如果包都位于不同的包中,则可以导入该包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.