Java元编程
我正在用 Java 开发我的第一个真正的项目。尽管我对动态语言有更多的经验,但我开始熟悉这门语言。
我有一个行为类似于以下内容的类:
class Single
{
public void doActionA() {}
public void doActionB() {}
public void doActionC() {}
}
然后我有一个 SingleList
类,它充当这些类的集合(具体来说,它用于 2D Sprite 库,并且“操作”都是各种变换:旋转、剪切、缩放等)。我希望能够执行以下操作:
class SingleList
{
public void doActionA() {
for (Single s : _innerList) {
s.doActionA();
}
}
... etc ...
}
是否有任何方法可以简单地将方法(或已知的方法列表)推迟到内部列表的每个成员?有什么办法不需要专门列出每个方法,然后循环每个内部成员并手动应用它?
让事情变得更困难的是,这些方法的数量不同,但返回类型都是“void”。
I'm working on my first real project with Java. I'm beginning to get comfortable with the language, although I have more experience with dynamic languages.
I have a class that behave similar to the following:
class Single
{
public void doActionA() {}
public void doActionB() {}
public void doActionC() {}
}
And then I have a SingleList
class that acts as a collection of these classes (specifically, it's for a 2D Sprite library, and the "actions" are all sorts of transformations: rotate, shear, scale, etc). I want to be able to do the following:
class SingleList
{
public void doActionA() {
for (Single s : _innerList) {
s.doActionA();
}
}
... etc ...
}
Is there any way to simply defer a method (or a known list of methods) to each member of the inner list? Any way without having to specifically list each method, then loop through each inner member and apply it manually?
To make things a bit harder, the methods are of varying arity, but are all of return type "void".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,Java 并不容易支持在运行时创建类,而这正是您所需要的:需要使用必要的存根方法自动更新
SingleList
以匹配Single
类。我可以想到以下解决此问题的方法:
使用Java 反射:
SingleList
类将不再与Single
类接口兼容。使用构建系统和某种源代码生成器来自动创建
SingleList.java
文件。SingleList
类实际上与加载的Single
类匹配。手动解决这个问题 - 创建一个接口(例如
SingleInterface
)或一个供两个类使用的基本抽象类应该会有所帮助,因为任何像样的 IDE 都会指出未实现的方法。正确的类架构将最大限度地减少重复代码,并且您的 IDE 可能能够帮助生成样板部分。使用字节码生成库,例如 Javassist 或 < a href="http://commons.apache.org/bcel/" rel="noreferrer">BCEL 动态生成/修改
SingleList
类即时。Unfortunately Java does not readily support class creation at runtime, which is what you need: the
SingleList
needs to be automatically updated with the necessary stub methods to match theSingle
class.I can think of the following approaches to this issue:
Use Java reflection:
SingleList
class would not be compatible with theSingle
class interface any more.Use a build system along with some sort of source code generator to automatically create the
SingleList.java
file.SingleList
class loaded in any JVM - or your IDE, for that matter - actually matches the loadedSingle
class.Tackle this issue manually - creating an interface (e.g.
SingleInterface
) or a base abstract class for use by both classes should help, since any decent IDE will point out unimplemented methods. Proper class architecture would minimize the duplicated code and your IDE might be able to help with generating the boilerplate parts.Use a bytecode generation library such as Javassist or BCEL to dynamically generate/modify the
SingleList
class on-the-fly.