为每个新类分配唯一的标识符(不是实例!)

huangapple go评论161阅读模式
英文:

Assign unique ID for every new Class (not instance!)

问题

我在想,是否有可能为每个新类(而不是实例)分配一个唯一的ID。

我所指的示例:

  1. public class ChildObjectOne extends SuperClass {}
  2. public class ChildObjectTwo extends SuperClass {}
  3. public class SuperClass {
  4. private final int ID;
  5. public SuperClass() {
  6. this.ID = newID();
  7. }
  8. }
  9. final ChildObjectOne childObjectOne = new ChildObjectOne();
  10. final ChildObjectTwo childObjectTwo = new ChildObjectTwo();
  11. System.out.println(childObjectOne.getID()); //打印 1
  12. System.out.println(childObjectTwo.getID()); //打印 2
  13. childObjectOne = new ChildObjectOne();
  14. childObjectTwo = new ChildObjectTwo();
  15. System.out.println(childObjectOne.getID()); //打印 1
  16. System.out.println(childObjectTwo.getID()); //打印 2

我希望的是,它应该再次打印1和2。它应该为每个新类生成一个新的ID,但是如果我创建该类的新实例,我希望ID保持不变。

我尝试使用泛型来实现:

  1. public class SuperClass<T> {
  2. private static int ID;
  3. public SuperClass() {
  4. this.ID = newID();
  5. }
  6. }
  7. public class ChildObjectOne extends SuperClass<ChildObjectOne> {}
  8. public class ChildObjectTwo extends SuperClass<ChildObjectTwo> {}

我希望通过传递不同的T作为新类来计数,但这并没有起作用。相反,ID是上次设置的ID。

如何实现这种类型的ID系统呢?

英文:

I was wondering, whether it would be possible to assign a unique ID for every new class (not instance!).

Example of what I mean:

  1. public class ChildObjectOne extends SuperClass {}
  2. public class ChildObjectTwo extends SuperClass {}
  3. public SuperClass {
  4. private final int ID;
  5. public SuperClass() {
  6. this.ID = newID();
  7. }
  8. }
  9. final ChildObjectOne childObjectOne = new ChildObjectOne();
  10. final ChildObjectTwo childObjectTwo = new ChildObjectTwo();
  11. System.out.println(childObjectOne.getID()); //prints 1
  12. System.out.println(childObjectOne.getID()); //prints 2
  13. childObjectOne = new ChildObjectOne();
  14. childObjectTwo = new ChildObjectTwo();
  15. System.out.println(childObjectOne.getID()); //prints 3
  16. System.out.println(childObjectOne.getID()); //prints 4

What I want it to do instead is print 1 and 2 again. It should generate a new ID for every new class, but if I create a new instance of that class, I want the ID to stay the same.

I tried to achieve this using generics:

  1. public SuperClass&lt;T&gt; {
  2. private static int ID;
  3. public SuperClass() {
  4. this.ID = newID();
  5. }
  6. }
  7. public class ChildObjectOne extends SuperClass&lt;ChildObjectOne&gt; {}
  8. public class ChildObjectTwo extends SuperClass&lt;ChildObjectTwo&gt; {}

I was hoping, it would count passing a different T as a new class, but that didn't work. Instead the ID is the last one that was set.

How can I achieve this kind of ID system?

答案1

得分: 1

Class::getName

每个Java类已经带有一个唯一标识符:类的全限定名。无需您添加标识符。

通过调用Class::getName来访问全限定名。

对于一个类:

  1. String.class.getName()

>"java.lang.String"

对于一个对象(实例),调用Object::getClass,然后调用Class::getName

  1. customer.getClass().getName()

>com.example.invoicing.Customer

英文:

Class::getName

Each class in Java already carries a unique identifier: the fully-qualified name of the class. No need for you to add an identifier.

Access the fully-qualified name by calling Class::getName.

For a class:

  1. String.class.getName()

>"java.lang.String"

For an object (an instance), call Object::getClass, and then Class::getName.

  1. customer.getClass().getName()

>com.example.invoicing.Customer

答案2

得分: 1

为了扩展我的评论,类名将为您提供唯一的字符串ID。如果您希望该ID成为一个数字,您可以像这样做:

  1. class IdGenerator{
  2. private static int counter = 0;
  3. private static HashMap<String,Integer> classIdMap = new HashMap<>();
  4. public static synchronized int getId(Class clazz){
  5. if (classIdMap.containsKey(clazz.getName())) {
  6. return classIdMap.get(clazz.getName());
  7. } else {
  8. classIdMap.put(clazz.getName(), ++counter);
  9. return counter;
  10. }
  11. }
  12. }

然后在您的类中,您可以执行以下操作:

  1. IdGenerator.getId(this.getClass());

生成的ID在每次运行应用程序时可能不会相同,这取决于其结构。例如:

  1. Scanner in = new Scanner(System.in);
  2. if (in.nextInt() < 100) {
  3. System.out.println("a = " + new Aclass().id); // a = 1
  4. System.out.println("b = " + new Bclass().id); // b = 2
  5. } else {
  6. System.out.println("b = " + new Bclass().id); // b = 1
  7. System.out.println("a = " + new Aclass().id); // a = 2
  8. }
英文:

To expand upon my comment, the class name will give you an unique String ID. If you want that ID to be a number you could do something like this:

  1. class IdGenerator{
  2. private static int counter = 0;
  3. private static HashMap&lt;String,Integer&gt; classIdMap = new HashMap&lt;&gt;();
  4. public static synchronized int getId(Class clazz){
  5. if (classIdMap.containsKey(clazz.getName())) {
  6. return classIdMap.get(clazz.getName());
  7. } else {
  8. classIdMap.put(clazz.getName(), ++counter);
  9. return counter;
  10. }
  11. }
  12. }

And then from your class you would do:

  1. IdGenerator.getId(this.getClass());

The generated IDs might not be the same every time you run your app, depending on how it is structured.
For example:

  1. Scanner in = new Scanner(System.in);
  2. if (in.nextInt() &lt; 100) {
  3. System.out.println(&quot;a = &quot; + new Aclass().id); // a = 1
  4. System.out.println(&quot;b = &quot; + new Bclass().id); // b = 2
  5. } else {
  6. System.out.println(&quot;b = &quot; + new Bclass().id); // b = 1
  7. System.out.println(&quot;a = &quot; + new Aclass().id); // a = 2
  8. }

huangapple
  • 本文由 发表于 2020年3月16日 06:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/60698370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定