Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用groovy父类方法-Grails_Grails_Groovy - Fatal编程技术网

调用groovy父类方法-Grails

调用groovy父类方法-Grails,grails,groovy,Grails,Groovy,我有一个Grails服务 父类: class BarService{ def fetchData(params) { return fooData.toString() } } class FooService extends BarService{ def fetchData(params) { def fooData = super.fetchData(params) //this will call the BarService method

我有一个Grails服务

父类:

class BarService{

  def fetchData(params) {

    return fooData.toString()

  }

}
class FooService extends BarService{

  def fetchData(params) {

    def fooData =  super.fetchData(params) //this will call the BarService method

    return fooData 
  }

}
子类:

class BarService{

  def fetchData(params) {

    return fooData.toString()

  }

}
class FooService extends BarService{

  def fetchData(params) {

    def fooData =  super.fetchData(params) //this will call the BarService method

    return fooData 
  }

}
这是正确的常规方法吗?因为对我来说这看起来像Java


谢谢

根据您的示例,除了删除可选的
return
关键字外,没有什么可以做的:

// Parent Class:

class BarService{
  def fetchData(params) {
    params.fooData.toString()
  }
}

// Child Class:

class FooService extends BarService{
  def fetchData(params) {
    super.fetchData params
  }
}


assert new FooService().fetchData([fooData: 900]) == "900"
“return”关键字不是问题所在(正如你在这里看到的-)。如果您得到的是“null”,则问题在于以下代码:

return fooData.toString()
应该像@WillP所说的那样(可选带有“return”键):