簡體   English   中英

一個groovy腳本可以繼承Java或Groovy超類嗎?

[英]Can a groovy script inherit from a Java or Groovy superclass?

這是腳本(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)

怎么會這樣?

腳本可以使用CompilerConfiguration擴展基類。 需要注意的是,基類必須擴展腳本,因為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.'

如果包都位於不同的包中,則可以導入該包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM