常规变量函数

发布于 2024-08-30 01:34:27 字数 556 浏览 2 评论 0原文

我什至不知道如何给这个 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 are
funA(), 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-09-06 01:34:27

是的,这是可能的。这应该可以满足您的要求:

service."fun${var}"()

正确的标题是动态方法调用

Yes, this is possible. This should do what you want:

service."fun${var}"()

The correct title is dynamic method invocation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文