java - Generating unique auto-incrementing instance IDs -
I'm trying to create unique IDs for examples of class starting with 1 like:
public abstract class foo {static id = 0; Final intestinal id; Public foo () {this.id = ++ id; }} Then I realized that I have many classes that require this behavior, and so I tried to inherit a abstract class But I got it >
Edit:
/ Em>I did not clarify this, but for each subclass The requirement of indexing is not only unique, but also continuously.
For example, if I inherited from the bar and baz foo :
bar b1 = new bar (); Falcon B2 = new falcon (); Bar b3 = new bar (); should be b2 id == 1 , and b3 should be id == 2 .
Not really! There are some things you can try:
- Create a
IDGeneratorclass with a static method to create a new ID for a specific code..generate ()method inclassHashmap mapping will retain sections. It will keep all logic in one class, but in reality does not simplify things. - Use a similar reference for each new instance of a
IDGeneratorclass in this class. Use this behavior, in this case,IDGeneratorCategory toclassmethod will be a non-argument method. Again, I'm not sure that things make things a lot easier..generate () - Skip the need for continuous id. In this way, you can have a class
IDGeneratorwith a static non-RGR.generate ()method that will only dish next to the unused identifier your identifier in your application Will be unique, and will not be consistent within a class. - Use a similar reference for each new instance of a
What you got is very silly-proof and slimline.
Another minor note: You say that you want your ID to begin with 0, but the way you wrote it, they will start with 1. You want to replace this.id = ID ++ instead of .id = ++ id .
Comments
Post a Comment