替代方法“将项目添加到枚举器”?
我的应用程序设计中有一组城市,我想将它们添加到枚举中。大部分时间我需要在这些城市工作一年左右。 然而,城市列表迟早会增长,这将需要向枚举添加元素。
据我了解,没有“好的”方法可以将元素添加到枚举中,似乎存在某些黑客行为,但目前看来最好远离它们。
那么在这种情况下我应该采取什么方法呢?我可以使用纯字符串,但我想要更好的东西。
I have my application design where I have a set of cities and I want to add them in a Enum. Mostly for a year or so those are the cities I will need to work on.
However the city list will grow sooner or later, which will then require to add elements to enum.
From what I understand there is no 'nice' way to add elements to an Enum, there seen to be certain hacks but for now it looks as if it is better to steer away from them.
So what should be my approach in this case? I could use plain String but I would want something better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接使用 Set 来实现这一点呢?并使用集合的迭代器枚举所有城市。
枚举并非设计为动态更新。它在概念上本质上是静态的
Why not just use a Set for this. And use the set's iterator to enumerate over all the cities.
Enum is not designed to be dynamically updated. its conceptually static in nature
你可能会使用一种享元方法(如果我没记错的话),而你有一个带有私有构造函数的类(类似于 Singleton),并且该类将管理其自身的所有实例 - 系统中将有一个实例对于每个城市,所有这些都由类本身管理,因此您可以使用 == 而不是 equals(),就像使用枚举一样。
you could probably use a flyweight approach (if my memory serves me correctly) whereas you have a class with a private constructor (similar to Singleton) and this class will manage all the instances of itself -- there will be one single instance in the system for each city, all managed by the class itself, and as such you can use == instead of equals() just as you would with enum.