帮我解决一下 FPC 中虚拟静态场可以解决的问题
我正在 Freepascal 中做一个事件管理器
每个事件都是一个对象类型TEvent(=对象),每种事件都必须从该类派生。
事件通过动态分配的整数标识符来区分。
问题是我想检索实例的事件 id,但我做得不好。
- 类(对象)的所有实例都有一个唯一的 id =>所以它应该是静态字段。
- 所有类都有不同的 id =>所以它应该是虚拟的。
- 事件 ID 在运行时分配,并且可以更改 =>所以这不是一个简单的方法
总之,我无法将所有这些放在一起。
我正在寻找一个优雅的解决方案,我不想编写一个硬编码表,在每个构造函数中实现它......等等,我更喜欢利用多态性的东西
任何人都可以帮助我提供其他技术或设计解决方案吗?
我说我不想使用类而不是对象构造。(属性不适用于对象?:(
I'm doing an event manager in Freepascal
Each event is an object type TEvent (=object), each kind of event must derive from this class.
Events are differentiated by an integer identificator, assigned dynamically.
The problem is that i want to retrieve the event id of an instance, and i can't do it well.
- All instances of a class(object) have a unique id => so it should be static field.
- All classes have a diferent id => so it should be virtual.
- Event ids are assignated in run time, and can change => so it can't be a simple method
In sum, I can't put all this together.
I'm looking for an elegant solution, i don't want to write a hardcoded table, actualizing it in every constructor... etc, i'd prefer something taking advantage of the polymorphism
Can anyone help me with another technical or design solution?
I remark I don't want to use class instead of object construct.(property doesn't work on objects? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要类 var 之类的东西,就像在较新的 delphi 中一样。但这仅适用于 FPC 的开发版本(2.5.1+)。
请注意,对象类型是 TP 遗留的,并且在本世纪还没有开发出来,我不希望这种情况发生改变。如果您需要的内容超出了它提供的内容,我建议使用课程。
You might need class var like stuff, like in newer delphi's. But that is in the development version of FPC only (2.5.1+).
Note that the object type is TP legacy, and hasn't been developed on in this millenium, and I don't expect that to change. If you need more than it offers, I suggest to use classes.
你可以像这样制作一个简单的表格/列表:
然后你可以获取/设置 id 祝
你好运
You can make a simple table/list like this:
Then you can get/set the id with
Good luck