我在Groovy脚本中有继承问题.我希望我的Groovy脚本从我调用此脚本的Java类继承方法.
例如,我有这样的事情:
public class SimpleTest extends TestCase {
public void test(){
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.setScriptBaseClass(this.getClass().getName());
GroovyShell shell = new GroovyShell(this.getClass().getClassLoader(), new Binding(), configuration);
shell.evaluate("println sayHello()");
}
public String sayHello(){
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
org.codehaus.groovy.control.MultipleCompilationErrorsException:startup failed:Script1.groovy:1:声明类型com.test.SimpleTest不扩展groovy.lang.Script类!@第1行,第1列.println sayHello()^ 1错误
如果我不能继承任何其他类,我怎么能这样做?我只想从超类中调用方法.
编辑
我改变了我的课程:
public class CmTest extends TestCase {
public void test(){
GroovyHandler handler = new GroovyHandler();
handler.run();
}
public String sayHello(){
return "Hello";
}
public class GroovyHandler extends Script {
public GroovyHandler(){
}
@Override
public Object run() {
CompilerConfiguration …Run Code Online (Sandbox Code Playgroud)