常规变量函数
我什至不知道如何给这个 qn 命名。但是,希望有一种简单的方法可以用像 Groovy 这样的动态语言来做到这一点。
假设我有一个服务类,我在其中委托业务逻辑。里面的方法有 funA()、funB()...funX()
。
现在我有一个控制器类,我在其中调用服务闭包,可以像 service.funA() 一样调用它。现在基于一个变量(可以有值A, B ... X
),我需要计算正确的服务闭包。基本上是为了避免编写大量 if 条件语句。像 service."fun+var"()
这样的东西就可以了。我不确定是否可以替换闭包(函数)名称中的变量。或以任何方式传递函数(名称)作为参数...不确定
我认为 PHP 有类似的功能 http://php.net/manual/en/functions.variable-functions.php
感谢您的任何指示..
I'm not even sure about how to title this qn. But, hope there is an easy way to do it in dynamic language like groovy.
say I have a class Service, where I delegate the business logic. the methods in it arefunA(), funB()... funX()
.
Now I have a controller class, where I call the service closure, which can be invoked like service.funA() . Now based on a variable (which can have values A, B ... X
), I need to cal the correct service closure. Basically to avoid writing lot of if conditional statements. Something like service."fun+var"()
would do. I'm not sure whether it is possible to substitute variable in closure (function)name. or any way by passing function (name) as a parameter...not sure
I think PHP has a similar feature http://php.net/manual/en/functions.variable-functions.php
thanks for any pointer..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。这应该可以满足您的要求:
正确的标题是动态方法调用。
Yes, this is possible. This should do what you want:
The correct title is dynamic method invocation.