简体   繁体   English

调用groovy父类方法 - Grails

[英]Call groovy parent class method - Grails

I have a Grails service, 我有Grails服务,

Parent Class: 家长班:

class BarService{

  def fetchData(params) {

    return fooData.toString()

  }

}

Child Class: 儿童班:

class FooService extends BarService{

  def fetchData(params) {

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

    return fooData 
  }

}

Is this the right groovy way of doing it? 这是正确的groovy方式吗? Because to me this looks like Java 因为对我来说这看起来像Java

Thanks 谢谢

As per your example, there is not much that can be done, except maybe removing the optional return keyword: 根据您的示例,除了可能删除可选的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"

The "return" keyword isn't the problem (as U can see here - https://groovyconsole.appspot.com/edit/5158539458772992 ). “return”关键字不是问题(如你所见 - https://groovyconsole.appspot.com/edit/5158539458772992 )。 If You're getting a "null", the problem is the code at: 如果你得到一个“null”,问题是代码:

return fooData.toString()

Should be like the @WillP said (with "return" keywork optionally): 应该像@WillP所说的那样(可选择“返回”关键字):

return param.fooData.toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM