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

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

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

问题

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

我所指的示例:

public class ChildObjectOne extends SuperClass {}
public class ChildObjectTwo extends SuperClass {}

public class SuperClass {
   private final int ID;

   public SuperClass() {
      this.ID = newID();
   }
}

final ChildObjectOne childObjectOne = new ChildObjectOne();
final ChildObjectTwo childObjectTwo = new ChildObjectTwo();

System.out.println(childObjectOne.getID()); //打印 1
System.out.println(childObjectTwo.getID()); //打印 2

childObjectOne = new ChildObjectOne();
childObjectTwo = new ChildObjectTwo();

System.out.println(childObjectOne.getID()); //打印 1
System.out.println(childObjectTwo.getID()); //打印 2

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

我尝试使用泛型来实现:

public class SuperClass<T> {
   private static int ID;

   public SuperClass() {
      this.ID = newID();
   }
}

public class ChildObjectOne extends SuperClass<ChildObjectOne> {}
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:

public class ChildObjectOne extends SuperClass {}
public class ChildObjectTwo extends SuperClass {}

public SuperClass {
   private final int ID;

   public SuperClass() {
      this.ID = newID();
   }
}

final ChildObjectOne childObjectOne = new ChildObjectOne();
final ChildObjectTwo childObjectTwo = new ChildObjectTwo();

System.out.println(childObjectOne.getID()); //prints 1
System.out.println(childObjectOne.getID()); //prints 2

childObjectOne = new ChildObjectOne();
childObjectTwo = new ChildObjectTwo();

System.out.println(childObjectOne.getID()); //prints 3
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:

public SuperClass&lt;T&gt; {
   private static int ID;

   public SuperClass() {
      this.ID = newID();
   }
}

public class ChildObjectOne extends SuperClass&lt;ChildObjectOne&gt; {}
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来访问全限定名。

对于一个类:

String.class.getName()

>"java.lang.String"

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

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:

String.class.getName()

>"java.lang.String"

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

customer.getClass().getName()

>com.example.invoicing.Customer

答案2

得分: 1

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

class IdGenerator{
    private static int counter = 0;
    private static HashMap<String,Integer> classIdMap = new HashMap<>();
    
    public static synchronized int getId(Class clazz){
        if (classIdMap.containsKey(clazz.getName())) {
            return classIdMap.get(clazz.getName());
        } else {
            classIdMap.put(clazz.getName(), ++counter);
            return counter;
        }
    }
}

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

IdGenerator.getId(this.getClass());

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

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

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:

class IdGenerator{
    private static int counter = 0;
    private static HashMap&lt;String,Integer&gt; classIdMap = new HashMap&lt;&gt;();
    
    public static synchronized int getId(Class clazz){
        if (classIdMap.containsKey(clazz.getName())) {
            return classIdMap.get(clazz.getName());
        } else {
            classIdMap.put(clazz.getName(), ++counter);
            return counter;
        }
    }
}

And then from your class you would do:

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:

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

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:

确定