调用groovy父类方法-Grails
我有一个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
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”键):