是否可以使用类别来“引导”一堆相关的类别?
我有一个我想要“使用”的类别类,它作用于第三方服务类。服务类有一堆 xxxRequest 和 xxxResponse 类以及用于每个 xxx 调用的数据的附加子类。
我想在应用服务类别的同时自动将类别应用到每个附加类,且范围相同。
我可以列出 use 块中的所有类别类,但有很多,我不想公开该细节。基类不受我的控制,因此我无法使用@category/@mixin。
目前,我的服务类别中有一个静态初始化方法,它执行一系列 mixin 调用(以及添加奇怪的额外构造函数)。这工作正常,但没有提供我想要的范围。
有没有我错过的更简单的方法?
I have a category class that I want to "Use" that acts on a third-party service class. The service class has a bunch of xxxRequest and xxxResponse classes plus additional child classes for data for each xxx call.
I want to automatically apply categories to each of the additional classes at the same time as the service category is applied, with the same scope.
I could list all the category classes in the use block but there are a lot and I don't want to expose that detail. The base classes are out of my control so I can't use @category/@mixin.
Currently I have a static initialise method in my service category that does a series of mixin calls (as well as adding the odd extra constructor). This works ok but doesn't provide the scoping I want.
Is there an easier way that I have missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我明白你想做什么。如果您尝试在同一代码块中同时
使用
多个类别,但您不想每次使用它们时都列出所有这些类别(如果不是这样,请纠正我)你的意思是),你可以把那个长的use
放在一个以代码块(闭包)作为参数的方法中,然后使用该方法而不是长的use
。类似于:在这个愚蠢的示例中,String 和 Integer 类将是您想要增强但您无权访问的类,而
useAwesomeExtensions
的作用就像一个包含一堆内容的大类别其他类别的。希望有帮助:)I'm not sure I understand what you are trying to do. If you are trying to
use
many categories at once in the same block of code but you don't want to list all those categories each time you want to use them (please correct me if that is not what you meant), you can put that longuse
inside a method that takes a block of code (a Closure) as a parameter and then use that method instead of the longuse
. Something like:In this silly example, the String and Integer classes would be the classes that you want to augment but you don't have access to, and the
useAwesomeExtensions
acts like a big category that includes a bunch of other categories. Hope that helps :)